Windows App Development

Background Tasks

Background tasks allow your Universal Windows Platform (UWP) app to perform operations even when it's not actively running. This is crucial for scenarios like updating live tiles, synchronizing data, or responding to system events without requiring the user to open your app.

Understanding Background Tasks

UWP apps can register and execute background tasks that are triggered by specific system events or conditions. These tasks are managed by the operating system to ensure that system resources are used efficiently. Key concepts include:

Types of Background Tasks

UWP supports several types of background tasks, each suited for different scenarios:

Registering a Background Task

To register a background task, you typically create a background task class that implements the IBackgroundTask interface and then use the BackgroundTaskRegistration API.


using Windows.ApplicationModel.Background;

// ... within your app's code ...

async void RegisterBackgroundTask()
{
    var result = await BackgroundExecutionManager.RequestAccessAsync();
    if (result != BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity &&
        result != BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
    {
        // Handle cases where access is denied or restricted.
        return;
    }

    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
    builder.TaskEntryPoint = "MyBackgroundTasks.MyBackgroundTask"; // Namespace.ClassName
    builder.SetTrigger(new TimeTrigger(15)); // Trigger every 15 minutes
    builder.Register();
}
            

Implementing the Background Task Entry Point

Your background task must have an entry point class that implements the IBackgroundTask interface. This class will contain the logic that runs when the task is activated.


using Windows.ApplicationModel.Background;

namespace MyBackgroundTasks
{
    public sealed class MyBackgroundTask : IBackgroundTask
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // Get a deferral to keep the task running even after the Run method returns.
            var deferral = taskInstance.GetDeferral();

            try
            {
                // Your background task logic goes here.
                // For example, update a live tile or sync data.
                System.Diagnostics.Debug.WriteLine("MyBackgroundTask is running...");

                // Perform your task operations...
            }
            finally
            {
                // Release the deferral when your task is complete.
                deferral.Complete();
            }
        }
    }
}
            
Tip: Always obtain a deferral when your background task needs to perform asynchronous operations or requires more time to complete than the default execution time.

Background Task Constraints and Best Practices

Note: The availability and behavior of background tasks can vary depending on the device's power state and system configuration. Test your background tasks thoroughly on various devices and under different conditions.

Further Reading