Register Background Task in Silverlight 8.1 app

Registering a BackgroundTask is quite well explained here at MSDN.

Here is simple example fired upon TimeTrigger and showing a Toast, the steps are (applies to both – RunTime and Silverlight apps):

    1. BackgroungTask must be a Windows Runtime Componenet (no matter if your App is Runtime or Silverlight). To add a new one, right click on your Solution in Solution Explorer window in VS, select Add then New project and choose Windows Runtime Component.

    winRTcomponent

    2. Add a reference in your main project.

    addreference

    3. Specify Declarations in Package.appxmanifest file – you need to add a Backgorund Task, mark Timer and specify Entry Point for the Task. The Entry Point will be a Namespace.yourTaskClass (which implements IBackgroundTask) – the added Windows Runtime Component.

    declaration

    4. How can your BackgroundTask look like? – let’s say we want to send a Toast from it (of course it can be many other things):

    namespace myTask // the Namespace of my task 
    {
       public sealed class FirstTask : IBackgroundTask // sealed - important
       {
          public void Run(IBackgroundTaskInstance taskInstance)
          {
            // simple example with a Toast, to enable this go to manifest file
            // and mark App as TastCapable - it won't work without this
            // The Task will start but there will be no Toast.
            ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
            XmlNodeList textElements = toastXml.GetElementsByTagName("text");
            textElements[0].AppendChild(toastXml.CreateTextNode("My first Task"));
            textElements[1].AppendChild(toastXml.CreateTextNode("I'm message from your background task!"));
            ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
          }
       }
     }
    

    5. Finally, let’s register our BackgroundTask in main project:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        // Windows Phone app must call this to use trigger types (see MSDN)
        await BackgroundExecutionManager.RequestAccessAsync();
    
        BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "First Task", TaskEntryPoint = "myTask.FirstTask" };
        taskBuilder.SetTrigger(new TimeTrigger(15, true));
        BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
    }
    

Compile, run and it should work. As you can see the task should start after 15 minutes (this time can vary as OS schedules task in specific intervals, so it will fire between 15-30 minutes). But how to debug a task faster?

There is a simple way – go to Debug location toolbar and you will see a dropdown Lifecycle events, choose your task from it and it will fire (sometimes open/close dropdown to refresh it).

run faster

Here you can download my sample code – WP8.1 Silverlight App.

Leave a Comment