Just as with traditional Windows apps, you will at times need to perform operations in the background. That is to say operations that need to be performed when your app is not running. Traditionally, you would either create a Windows Service and/or an app that automatically starts upon login. Unfortunately, for Windows 8 Metro Apps it is not possible to do either of these options. However, it does provide support for a new method known as Background Tasks.
Background Tasks for Windows 8 Metro Apps allow you to execute code triggered via one of several triggers, such as Time, Maintenance, Push Notification, etc. In addition, you can also specify one or more Conditions such as Internet Available, User Present, etc. To get started, we first need to create the Background Task itself. Listed below is a simple TimerTask, which implements the IBackgroundTask interface.
public sealed class TimerTask : IBackgroundTask { public TimerTask() { } async void IBackgroundTask.Run(IBackgroundTaskInstance taskInstance) { taskInstance.Canceled += taskInstance_Canceled; //Since the Run method is asnc we need use a deferral BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); // put your code with await statements here. deferral.Complete(); } void taskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { //Put code here to handle a cancel scenario } }
This class implements the required Run method for the IBackgroundTask interface as well as a cancelled event handler, which is commonly needed for Background Tasks. You will need to place the code you wish to execute within the Run method as identified. Before your Background Task can be used it must be defined in the Package.appxmanifest included with your project. On the Declarations tab, you will need to add a Background Task. Be sure to check Timer under the Properties area and populate the Entry point with the namespace and class name for the BackgroundTask, such as BackgroundTask.TimerTask. Before the task can be executed, you will need to register it when your application is launched.
private bool RegisterBackgroundTask() { BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = "MyBackgroundTask"; builder.TaskEntryPoint = "BackgroundTask.TimerTask"; IBackgroundTrigger trigger = new TimeTrigger(15, false); builder.SetTrigger(trigger); builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); IBackgroundTaskRegistration task = builder.Register(); return true; }
This method uses the BackgroundTaskBuilder class to provide details about the task, including the name, entry point, triggers and conditions, etc. IBackgroundTaskRegistration object returned from the Register method can be used to provide feedback from the task such as Progress. Depending upon the purpose for your app, you may choose to register the Background Task when your application launches or after a certain set of conditions or configuration.
Conclusion
While the background task may seem almost like a direct replacement for the traditional Windows Service, unfortunately, this could not be further from the truth. For instance, while you can create a Background Task, which runs every 15 minutes, you are restricted at the amount of processing you can perform. Since Metro apps will often run on battery powered devices they are restricted from using excessive amounts of bandwidth and/or CPU resources. For further information regarding resource constraints and Background Tasks in general, consult the Introduction to Background Tasks document by Microsoft.