Multitasking in Windows Phone 7 (Agents, Reminders, Alarms)

Multitasking is a major part in Windows Phone 7 Mango, in the previous talk we’ve discussed a part of it the “Fast Application Switching”, if we think about it FAS is really a simulation of multi tasking since in reality deactivated apps do not keep running in the background whether they get tombstoned or frozen in memory. Multitasking is the ability to do more than one things at a time, which means, working can be done in the background while something else is running in the foreground.

In mango, multitasking needs the ability to start and experience in the foreground and continue to work in the background, or conversely to start something in the background and continue in the foreground. Now in the Windows Phone OS obviously does multi tasking internally such as syncing emails in the background or handling phone calls and so on. The point is multitasking has not been available to us third party developers and the reason for that is the key requirement on maintaining a balance between the phone’s user experience and system health which includes battery consumption, network efficiency and internal services CPU and memory performance.

Opening the Operating System to third party multitasking without restrictions could potentially have disaster consequences on that balance as it would make system health dependence on the individual applications that are installed on the phone. So to preserve the system health while still allowing tons of scenarios that background processing would enable the Windows Phone team has come up with an infrastructure that we can use to do such as background processing in our applications.

This infrastructure is comprised of a set of new operating system services, background agents which are transient services in our applications that can run when invoked by the system.

We’ll be examining agents in the next posts I will publish.

Enhance resource management system that monitors and maintain system health, so in our applications when we need to perform some work in the background we use AGENTS. Mango introduces several different agents that corresponds to our specific needs.


When working with Agents there are two aspects to consider:

1. Code that runs in the foreground

2. Code that runs in the background


Let’s look at the API that mango exposes as that will give as a overall picture of how the agents work.

On the foreground side, the top level abstraction that we use is the ScheduleAction class, this represents something that can run even if the application that scheduled something is no longer running.

There are two categories of ScheduleActiosn

1. ScheduleNotifications – includes reminders and alarms

2. ScheduleTask – also known as generic agents allow us our own custom code in the background at regular system controls

The difference between the two types of ScheduleTask, PeriodicTask and ResourceIntesiveTask is how often and how long the agent is run by the system.

We use the ScheduledActionService Class to schedule actions and tasks in the background and to retrieve update and remove currently scheduled actions.

Now aside from the foreground API, the code that needs to run in the background is contained within an Agent, if you want to develop your generic agent you need to use a class derived from ScheduledTaskAgent which will run in the background process, Scheduledtaskagent derives from Backgroundagent which we see as it is the base class for all of our agents.  In the foreground application we use the schedule task to schedule an agent, as a result our custom code will run in regular intervals regardless whether the foreground application that scheduled the task is running or closed.

In the next posts, I will show you how does Schedule Notification works.