Converting to Windows 8 from Windows Phone | Background agents (11 of 12)

The Mango (7.1) update for Windows Phone brought us a lot of goodies including the Scheduled Task Agent. It allowed us to create a feeling of push notifications without actually doing any server push implementation. In the Reseguiden app we use the background agents to do new searches for trips and displaying the top result in the app tile. Sort of a budget-push.

So, the question is, does the same functionality exist in Windows 8? The answer is yes; same same but different.

This post is a part in a series – you can find the full index here.

New times, new APIs

You will recognize the same thinking when looking at background agents for Windows 8 as in Windows Phone. They are declared in the app manifest and kept in a separate project. The big difference is that the control of the background running has been moved from .NET to the Windows Runtime. And since the logic for it has moved the way to call it has changed as well.

So, the plan is to implement a background agent in Windows 8, explain how it works, what we’re doing and how it differs from Windows Phone. Let’s begin.

Setting up the basics

First and foremost we need to activate our background process. We chose to create a class BackgroundTask and on that the method Register.

public class BackgroundTask { public void Register() { var builder = new BackgroundTaskBuilder(); builder.Name = "UpdateLiveTile"; builder.TaskEntryPoint = "Peach.Background.UpdateTile"; IBackgroundTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false); builder.SetTrigger(trigger); builder.Register(); } }

Worth noting here:

  • TaskEntryPoint is the full name of the class to be executed. We have not created that class yet. Important is that the class implements IBackgroundTask.
  • We’re creating a Trigger, in this case a SystemTrigger. As you can see we’re triggering on InternetAvailable. Other available triggers are:
    • TimeTrigger*
    • PushNotificationTrigger*
    • ControlChannelTrigger*
    • MaintenanceTrigger

    *) requires the app the be visible on the lock screen

  • You can (but we won’t) register an IBackgroundCondition that can e.g. require the user to be active.

We’ll call Register in the OnLaunched method of App.cs.

Now, let’s move on to our app manifest where we have to declare that we are running a background process. We’ll place the following piece of XML inside the Application element.




Not so much to say about it, this is how it looks.

Implementing the background process

In this case we will update the live tile from the background process. Before we get to code, we have to create the project to hold the background process. You just add a new project to the solution, we chose the name Reseguiden.Background.

Now to the important part. Go to the properties for the project and change Output Type from Class Library to WinMD file. The reason for this is that it’s the Windows Runtime that will execute the code in this project, and WinRT does not understand .NET. Let’s jump to our implementation of IBackgroundTask.

public sealed class UpdateTile : IBackgroundTask { void IBackgroundTask.Run(IBackgroundTaskInstance taskInstance) { XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareText02); XmlNodeList tileTextAttributes = tileXml.GetElementsByTagName("text"); foreach (var tileTextAttr in tileTextAttributes) { tileTextAttr.InnerText = "Någon text"; } var tileNotification = new TileNotification(tileXml); TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification); } }

As you can see we create a tile update, nothing magic here. Just regular code that will be executed regardless of if the app is suspended or not. Well a little magic there is; the class must be sealed.

Putting it all together

Now to the step that we forgot when implementing this. If the app were to be run now, the WIFI turned off and on again, the tile would not update, and the app would crash without any error messages…

Important part number 2. Add a reference to your WinMD-project (the one containing the implementation of IBackgroundTask) from your application project. That solves the crash problem.

So, as we brushed on earlier, to test your application you can.

  1. Start the app
  2. Put the app in the background (e.g. go to the home screen)
  3. Wait 10-15 seconds (so the app will be suspended)
  4. Disconnect the internet
  5. Reconnect the internet
  6. Look at the tile and watch it update itself

Das ist alles!

This Post Has 2 Comments

  1. Tanato

    There is any way to debug the backgroundtask?

Leave a Reply