Register Background Task in Silverlight 8.1 App

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.

Windows phone 8.1 background tasks

The answer above is wrong. Wonder how it has 5 votes. Anyways, background task cannot run in a silverlight applciation but it can only register a background task. It has to be a runtime project that runs a background task.

A silverlight application can just register a background task which is a separate runtime project. Its a different process all together.

windows phone 8 + Background work app

Normally when your app gets Suspened your is stopped by OS and can be even terminated when phone is low on resources (for more details see MSDN). You cannot do much with this, if the user decides to leave your app then it will be suspended.

In WP8.1 Background Tasks were introduced - short programs that can run in the bucground and be fired with certain triggers (in Windows Phone 8.0 (also WP8.1 Silverlight) there were background agents). Among those Tasks there are also some special ones, for example like Background Audio, which allows for continous work but with certain purpose.

So if you need some background work (when your app gets suspended), then you will have to build and register a Bacground Task - there are couple of resources in the internet, which may help:

  • MSDN - how to register a Background Task,
  • this SO question - it stays both for Runtime and Silverlight as Background Tasks can be used in both systems,
  • you may also take a look at this blog post.

Also remember that some apps (Skype for example) are developed by Microsoft or their partners and they can have access to different APIs other that official for average developers.

Windows phone 8.1 background task and foreground app communication

Here are some ideas (or info) about communication between the app and its background tasks.

  • You could use the Progress and Completed events of the IBackgroundTaskRegistration object. You can get that object using BackgroundTaskRegistration.AllTasks - this property returns the list of background tasks registered by the app. Each time the app runs, you'll have to subscribe to these events.

    From the background task you can set the Progress property of the IBackgroundTaskInstance object to some UInt32 value, and the app will receive the event. Maybe you can encode what you need in that number. For example: 1 means that the task is initializing, 2 - the task is doing WorkA, and so on...

  • Both processess have access to the same files, so maybe you can use that for something.

  • Use Mutex to sync the execution of code between the two processes.

That's all I can think of right now. I hope it helps.

P.S. I haven't really tried those events, but they seem like they might be useful.

Can I set the start background from a Windows Phone 8.1 (Store or Silverlight) app?

There is no API to set 'Start background' image from third part app. see this question answer comments by Claus Jørgensen

What is AgHost.BackgroundTask as background task in Windows Phone Silverlight 8.1?

AgHost.exe is the foreground process. This entry point is to allow you to host a background process, if necessary.

Unless you're writing a VoIP application, this should be perfectly safe to remove. However, if you're utilizing one or more background agents for GPS tracking, push notifications, or the likes, this might still be necessary.



Related Topics



Leave a reply



Submit