introduction

Background agents are scheduled tasks that run in the background and add value to your application that the other application might not have. Here we will cover everything about background agents, including their types and how and when to use them. I believe whatever app you have can be made better with the implementation of background agents, background agents keep the user in touch with the application when the application is not actually working (in other words not working in the foreground). We will look at the various ways in which we can use the background agents. Windows Phone has the philosophy "users first" and hence all the basic architecture depends on the best possible performance and battery life.

Foreground Tasks

As mentioned above, Windows Phone follows the philosophy "users first" so the application running in the foreground is the only application, there might be other applications that have lots of memory but no CPU utilization, that is the reason Windows Phone has the most fluid interface. This is where background agents are useful; as we said, the app in the foreground is the only app running but we also need some background processing for other apps like fetching emails and so on.

So instead of running the complete application in the background we have background agents that run on behalf of the application and perform specific tasks.

Background Agents

It is a totally different project in Visual Studio and is linked to the main application, it can interact with the background storage of the Windows Phone and can share data, but actually it is a totally different piece of code. On the basis of consuming processor resources we have two types of background agents:

There is only one agent allowed per application, and an agent can never be compared with the foreground application running in the background siince it has certain limitations. A background agent is limited in what it can do and the access it has to the processor and other phone facilities.

Background Agent Health Warning

The Operating System manages all the background agents, you can go to the settings and manage all your running background agents
as in the following:

Focus on the last point, background agents are the cherry on top and not the ice-cream. In other words it can just add value to your application and not provide the basic features, they are kept as light as possible. The Operating System manages all the background agents so as to ensure the behavior of each agent and hence no agents can behave abnormally to utilize more CPU or memory to disturb smooth execution of the applications.

Agents and Tasks

Basically a Task is the scheduling type you request when you schedule a background agent to run, and it is managed by the operating system that the background agent runs in the appointed time. As discussed, there are two kinds of Tasks:

PeriodicTask Agents

A periodic task runs every now and then, typically every 30 minutes or so, depending on the loading on the phone. It is intended to perform a task regularly and complete quickly. It is good for location tracking, polling background services and updating tiles.

Resource Intensive Agents

These are the agents that require high resources to do what they do, usually involving updating of the phone, downloading large files and so on. Whenever the processor feels it is able to provide access to the application the agents start running. A few features of Resource Intensive Agents are:

Dual purpose Agents

There are some dual purpose agents also, as the name implies they have the properties of both Periodic and Resource Intensive agents. Whenever the agent starts it determines in what context it is running and then behaves accordingly. The agent will run periodically and when the phone is in a position to allow resource intensive work it will switch it's mode.

Background Agent Functionality

As discussed above, some of the functionality is limited for the use of background agents and here is a list that shows all.

Allowed Restricted
Tiles Display UI
Toast Microphone and Camera
Location Sensors
Network Play Audio (may only use background audio APIs)
R/W Isolated Storage
Sockets
Most Framework APIs

This was all about Background agents in Windows Phone, in future articles we will learn how to implement toast notifications in applications.