Supporting your app with background tasks (Windows Store apps using C#/VB/C++ and XAML)
The topics in this section show how to run your own lightweight code in the background by responding to triggers with background tasks. Background tasks are lightweight classes that Windows runs in the background. You can use background tasks to provide functionality when your app is suspended or not running. You can also use background tasks for real-time communication apps like VOIP, mail, and IM.
Background tasks are separate classes that implement the IBackgroundTask interface. A Windows Store app registers background tasks by using the BackgroundTaskBuilder class. This class name is specified as the entry point while registering the background task.
To get started quickly with a background task, see Quickstart: Create and register a background task.
Background tasks for system events
Your app can respond to system-generated events by registering a background task with the SystemTrigger class. An app can use any of the following system event triggers (defined in SystemTriggerType) without being placed on the lock screen.
Trigger name | Description |
---|---|
InternetAvailable |
The Internet becomes available. |
NetworkStateChange |
A network change such as a change in cost or connectivity occurs. |
OnlineIdConnectedStateChange |
Online ID associated with the account changes. |
SmsReceived |
A new SMS message is received by an installed mobile broadband device. |
TimeZoneChange |
The time zone changes on the device (for example, when the system adjusts the clock for daylight saving time). |
For more info see How to respond to system events with background tasks.
Conditions for background tasks
You can control when the background task runs, even after it is triggered, by adding a condition. Once triggered, a background task will not run until all of its conditions are met. The following conditions (represented by the SystemConditionType enumeration) can be used.
Condition name | Description |
---|---|
InternetAvailable |
The Internet must be available. |
InternetNotAvailable |
The Internet must be unavailable. |
SessionConnected |
The session must be connected. |
SessionDisconnected |
The session must be disconnected. |
UserNotPresent |
The user must be away. |
UserPresent |
The user must be present. |
For more info see How to set conditions for running a background task.
Application manifest requirements
Before your app can successfully register a background task, it must be declared in the application manifest. For more info see How to declare background tasks in the application manifest.
Background tasks for lock screen-capable apps
Apps can be placed on the lock screen to show real-time information to the user at a glance. The following real-time triggers can be used to run lightweight custom code in the background for apps that are on the lock screen:
Control Channel: Background tasks can keep a connection alive, and receive messages on the control channel, by using the ControlChannelTrigger. For more info see How to use the control channel trigger.
Timer: Background tasks can run as frequently as every 15 minutes, and they can be set to run at a certain time, by using the TimeTrigger. For more info see How to run a background task on a timer.
Push Notification: Background tasks respond to the PushNotificationTrigger to receive raw push notifications. For more info see How to receive raw notifications.
Note The user must place your app on the lock screen before the app can use these background tasks. An app can request lock screen access by calling RequestAccessAsync. This presents a dialog box asking the user to "allow" or "don't allow" your app on the lock screen. An app is only allowed to ask for lock screen access once; any subsequent calls to RequestAccessAsync are ignored.
System event triggers for lock screen-capable apps
Note The SystemTriggerType enumeration includes the following system event triggers that are only usable by lock screen-capable apps. An app must be placed on the lock screen before it can register a background task with any of these system event triggers.
Trigger name | Description |
---|---|
UserPresent |
The background task is triggered when the user becomes present. |
UserAway |
The background task is triggered when the user becomes absent. |
ControlChannelReset |
The background task is triggered when a control channel is reset. |
SessionConnected |
The background task is triggered when the session is connected. |
The following system event triggers are also for lock screen-capable apps. These triggers can be registered even when the app is not on the lock screen, making it possible to recognize when the user has moved the app on or off the lock screen.
Trigger name | Description |
---|---|
LockScreenApplicationAdded |
An app tile is added to the lock screen. |
LockScreenApplicationRemoved |
An app tile is removed from the lock screen. |
Background task resource constraints
Background tasks are lightweight. Keeping background execution to a minimum ensures the best user experience with foreground apps and battery life. This is enforced by applying resource constraints to background tasks:
-
CPU usage is limited as follows.
CPU usage quota Refresh time App not on lock screen 1 second
2 hours
App on the lock screen 2 seconds
15 minutes
-
When running on battery (DC power), background tasks also have a network data usage limit. This limit is a function of the amount of energy used by the network interface, so it varies depending on the device and network environment - but it can be estimated.
The following table characterizes network data throughput, assuming a resource constrained WiFi network capable of 1Mbps of average data throughput. To estimate the correct limit, multiply by the average megabits per second (Mbps) of the connection. For example, an app can use 25 MB of data every 2 hours on a 10Mbps WiFi connection if it's placed on the lock screen. The example WiFi interface assumes minimal interference.
Refresh period 15 minutes 2 hours Daily Data limit (on lock screen) 0.469 MB n/a 45 MB Data limit (not on lock screen) n/a 0.625 MB 7.5 MB Note The network data usage limit is lifted when the device is plugged in (AC power), but the CPU usage quota still applies. Similarly, the CPU and network resource constraints are suspended for an app's background tasks while the user is interacting with it in the foreground.
Background task resource guarantees for real-time communication
To prevent resource quotas from interfering with real-time communication functionality, background tasks using the ControlChannelTrigger and PushNotificationTrigger receive guaranteed resource quotas (CPU and network) for every running task. The resource quotas and network data usage constraints are as mentioned above, and remain constant for these background tasks (rather than varying according to the power usage of the network interface).
Your app doesn't have to do anything differently to get the guaranteed resource quotas for ControlChannelTrigger and PushNotificationTrigger background tasks. The system always treats these as critical background tasks.
Maintenance trigger
Your app can also run tasks as frequently as every 15 minutes by using the maintenance trigger. Instead of requiring placement on the lock screen, maintenance tasks only run when the device is plugged in to AC power. For more info see How to use maintenance triggers.
Managing background tasks
Background tasks can report progress, completion, and cancellation to your app using events and local storage. Your app can also catch exceptions thrown by a background task, and manage background task registration during app updates. For more info see:
How to handle a cancelled background task
How to get a list of pending background tasks
How to monitor background task progress and completion
How to use the ServicingComplete trigger
Related topics
- Conceptual guidance for multitasking in Windows 8
- Launching, resuming, and multitasking
- UI guidance for the lock screen
- Displaying tiles on the lock screen
- Guidelines and checklist for lock screen tiles
- Related background task guidance
- Guidelines and checklist for background tasks
- How to debug a background task
- How to trigger suspend, resume, and background events in Windows Store apps (when debugging)