The next Win8 sample shows the general form of the background task contract for Windows 8 Metro applications.
There are two parts to implementing a background task for a Metro style application:
Implement the IBackgroundTask interface (contract).
Register the background task with the system so that Windows starts the task at the appropriate time.
The IBackgroundTask interface has only one method: Run(). The only parameter to Run() is an IBackgroundTaskInstance. Your background task will communicate with the Windows system through the IBackgroundTaskInstance. In this sample, the background task registers to receive the Cancelled event from the IBackgroundTaskInstance if it should stop working on the current problem. The background task also updates the IBackgroundTaskInstance.Progress property to notify the system of its progress on the current problem.
You can think of the IBackgroundTask.Run() method as similar to the Task.Run() method in today’s .NET environment: it’s the entry point for your background task.
Registering a background task in WinRT is a bit more involved. Remember that this background task is not the same as just running something asynchronously. Instead, these background tasks communicate with the OS, so that they can receive events while your application is suspended. For example, a Mail client will be awakened, check mail, then update its live tile periodically when suspended. That requires using the Windows system Background Task contract.
You register a Background Task in a few steps, using a BackgroundTaskBuilder object. There are several properties you need to set on the BackgroundTaskBuilder:
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
The name is simply a name you use to identify the task (uniquely) in your application. The TaskEntryPoint is the fully qualified class name for the type that implements IBackgroundTask.
Read more: SRT Solutions
QR: