In this article we are going to see a new feature of Windows Store application development which is utilizing Background tasks in the app development. In traditional application development the user controls when and how the application is running and positions to see how the application should run in parallel and access the resources and the system. With Windows Store app development, it’s something different that the system checks if the apps are actively running on the user screen or not. If the app is not used, the system will suspend the app in order to maintain performance and preserve system resources for other apps which gives more performance to the device.
In Windows Store app development, Background task is a simple class which are highly configurable and have a significant set of requirements that must be set to register and trigger correctly in the app. Background tasks are implements with IBackgroundTask interface which includes a single run method that passes IBackgroundTaskInstance which gives the task the path to speak with the current running process.
Background tasks are executed at the background to be first assigned to a single trigger which holds the responsibility to decide on how the task should be handled and executed accordingly. Background tasks are in different types that the tasks are triggered based on the need. The below table shows the list of tasks and the description on when to use those.
S No |
Trigger Name |
Trigger Description |
1 |
System |
It is responsible for system events which has a huge list of triggering options based on the requirement |
2 |
Time |
It is for time interval or task to trigger based on the time frame scheduled. |
3 |
Maintenance |
It is responsible for when the application is undergoing some maintenance or upgrade |
4 |
Push Notification |
It is for Windows Push Notification Services to send Raw Notifications as and when required |
5 |
Control Channel |
It is for channel communication based on the sockets that are available. |
With the list of available triggers, System triggers plays a major role in playing with Background Tasks in your application. System triggers are mainly available with a list of predefined events that can be used to register the task and run in the background. The list of events include
Trigger Name |
Description |
User Present | Triggered when the user is available |
User Away | Triggered when the user left the session |
ControlChannelReset | Triggered when control channel is hard resetted |
SessionConnected | Triggered when the session is connected |
LockScreenApplicationAddded | Triggered when a title is added to lock screen |
LockScreenApplicationRemoved | Triggered when a title is removed from the lock screen |
Writing your first Background Task Application:
public sealed class MyBackgroundTask : IBackgroundTask
{
private void UpdateTime()
{
// your business code
}
public void Run(IBackgroundTaskInstance taskInstance)
{
UpdateTime();
}
}
Background Tasks Best Practices (Microsoft MSDN Suggests)
- Design background tasks to be short lived.
- Design the lock screen user experience as described in the “Guidelines and checklists for lock screen tiles.”
- Do not specify the Executable attribute to ensure the task launches in the system-provided host.
- Describe the background task class name or JavaScript file name accurately in the manifest.
- Look in the event viewer for error messages if the background task is not being activated.
- Use persistent storage to share data between the background task and the app.
- Register for progress and completion handlers in the app.
- Register for a background task cancellation handler in the background task class.
- Register for the ServicingComplete trigger if you expect to update the app.
- Ensure that the background task class library is referenced in the main project and its output type is winmd.
- Describe the triggers in the background manifest accurately.
- Verify if the app needs to be on the lock screen.
- Do not display UI other than toast, tiles or badges from a background task.
- Do not rely on user interaction in background tasks.
Hope this gives some clear idea on what Background tasks are all about and how to achieve Background tasks in your Windows Store app development.