Periodic and ResourceIntensive Tasks in Windows Phone (Mango)
In this article we're going to look at the entree: Background Tasks. More specifically, Periodic and ResouceIntensive Tasks (There is one for audio and transfers, we'll look at these in the next article(s).
In my last
article, we looked at Alarms and Reminders, appetizers for the new scheduler
features in the new Mango update. Today we're going to look at the entree:
Background Tasks. More specifically, Periodic and ResourceIntensive Tasks (There
is one for audio and transfers, we'll look at these in the next article(s)).
Let's first look at the difference between the two types of tasks and where we
should use each. I'm sure by their names, you could fathom a guess as to what
each is meant to do, but here are the constraints for each, summarized from
their full descriptions at MSDNhttp://msdn.microsoft.com/en-us/library/hh202942(v=VS.92).aspx
Run when requirements are met
Battery Saver mode can prevent execution
External power required
Battery must be at 90%
Per-device periodic agent limit
Device must be locked
Non-cellular connection required (Wi-Fi or PC)
No active phone call
These constraints must be met for your tasks to
run. It is important to note that should any of these conditions no longer be
met, your task will be terminated, so make sure to write your tasks with this in
mind. It is ultimately up to you to choose which task best suites your
situation, but it is strongly recommended keeping periodic tasks as quick as
possible, while saving large synchronization tasks for a ResourceIntensiveTask. #define DEBUG_AGENT
Both tasks are restricted by a few universal rules. Firstly, they can't use
certain APIs (Mostly UI, calling other tasks, and interacting with the device's
sensors). The full list can be found at http://msdn.microsoft.com/en-us/library/hh202962(v=VS.92).aspx.
Secondly, the task may not use more than 5 MB of memory, or it will be
terminated. Lastly, your tasks have an ExpirationTime (the time that the system
will stop trying to execute it), which is by default 2 weeks. Anytime your
application is launched, you have an opportunity to extend that time, to the 2
With the prerequisites out of the way, let's look at how to use Tasks.
The first step in using ScheduledTasks is to create a ScheduledAgent class. This
will be invoked when your task is called. The class you write is only required
to be comprised of overloading OnInvoke(ScheduledTask task), as shown below. You
create this by adding a new project to the solution, selecting the new "Windows
Phone Scheduled Task Agent" temple, pictured here.
The project will open up and show you the template for the ScheduledAgent. Below
is what I see as the bare minimum for this class.
public class ScheduledAgent
protected override void OnInvoke(ScheduledTask
if (task is PeriodicTask)
Logic for Periodic Task
Logic for ResourceIntensiveTask
// Use this
to invoke your task much quicker when debugging
Tell the Scheduler we are done
As you can see, the "is" keyword is used to check what kind of task is calling
your agent, as all tasks will call the same one per application. We also set the
task to run one minute after being ran; a handy debugging feature. Finally, this
code tells the system that our task is officially completed.
The next step is to use this agent in our main Windows Phone project. Here is a
method used in the example at MSDN, which shows the entire process (The
procedure is the same for ResourceIntensiveTasks, just with a different class).
public partial class MainPage
string periodicTaskName = "PeriodicAgent";
string resourceIntensiveTaskName = "ResourceIntensiveAgent";
private void StartPeriodicAgent()
periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
// If the task already exists and the IsEnabled property is false, background
// agents have been disabled by the user
if (periodicTask != null && !periodicTask.IsEnabled)
MessageBox.Show("Background agents for this application have been disabled by the user.");
// If the task already exists and background agents are enabled for the
// application, you must remove the task and then add it again to update
// the schedule
if (periodicTask != null && periodicTask.IsEnabled)
periodicTask = new PeriodicTask(periodicTaskName);
// The description is required for periodic agents. This is the string that the user
// will see in the background services Settings page on the device.
periodicTask.Description = "This demonstrates a periodic task.";
// If debugging is enabled, use LaunchForTest to launch the agent in one minute.
This example first checks to see if the system
allows the task to run. It then checks if the task is still scheduled, and
removes it if it is. The important thing to not is that your task needs a name
and description. These are what the user will see in the device's settings for
background tasks. If they don't know what something is, they most likely will
prevent it from running, so be descriptive.
I know this was more talking than examples, but I feel that developers need a
good understanding of what they have to work with, before deciding which task we
need, or if we even need them at all. For a fully fleshed out example, I highly
suggest you take a look at the MSDN sample on this subject, over at
you're at it, check out the others, as they all go over new things Mango brings
to the table.