Background task guidance

Consider the following guidance when developing your background task, and before publishing your app.

Close your background tasks: Don't forget that the background task needs to call the close() method when it's done. If the background task doesn't close itself, the process running the background task can continue to consume memory and battery life even though the background task completed or was cancelled.

CPU and network quotas: Don't exceed the CPU quota or network data usage quota applied to your background task. Background tasks should be lightweight to save battery life and provide a better user experience for foreground apps. See Supporting your app with background tasks for the resource constraints applied to background tasks.

Update the app manifest: Declare each background task in the application manifest, along with the type of triggers it is used with. Otherwise your app will not be able to register the background task at runtime. For more information, see How to declare background tasks in the application manifest.

Prepare for app updates: If your app will be updated, create and register a ServicingComplete background task (see SystemTriggerType) to help perform app updates that may be necessary outside the context of running in the foreground.

Background tasks for lock screen-capable apps on Windows: The lock screen is a shared resource. Only seven apps may be placed on the lock screen at any one time, and only one may show a wide tile. Your app can provide a good user experience by requesting lock screen access using the RequestAccessAsync method, and by making sure your app will still work without being on the lock screen. Apps that are not placed on the lock screen can still update tiles, update badges, send notifications, and register for system event triggers. The user experience when your app is in the foreground should never be disrupted even if the user has not placed your app on the lock screen.

Read the Lock screen overview to get a sense for whether or not the lock screen is the right place for your app.

Requesting to execute background tasks for Windows Phone Store apps:

Windows Phone Store apps can run all supported task types without being pinned to the lock screen. However, your app must call RequestAccessAsync before registering any type of background task. This method will return BackgroundAccessStatus.Denied if the maximum number of apps with background tasks across the system has been exceeded or if the user has explicitly denied background task permissions for your app in the device's settings.

For Windows Phone Store apps, if your app will be updated, you must call RemoveAccess and then RequestAccessAsync when your app launches after being updated. To determine when your app has been updated, you should track the version number of your app using a value stored in local settings. When your app is launched, check the version of your app, and if it is newer than the version in local settings then call RemoveAccess and RequestAccessAsync. To do this, add code similar the following and call it from the launching event handler for your app.

Include a background task registered with TimeTrigger and declare it in the app manifest. Make sure the entry point and trigger types are correct. This is required for certification, and enables the user to place the app on the lock screen.

For Windows Phone Store apps, if the device becomes low on memory, background tasks may be terminated without any warning and without raising the OnCanceled event. This helps to ensure the user experience of the app in the foreground. Your background task should be designed to handle this scenario.