Understanding Windows Phone Background Agents

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

  • Normally a Windows Phone application runs in the "foreground".
     
    • Has access to the screen and interacts directly with the user of the phone.
     
  • At any given time only one application is running in the foreground.

    • Although others may be in the memory of the phone and can be selected as required.

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:

  • PeriodicTask
  • ResourceIntensiveTask

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:

  • The number of background agents is limited per application (in other words 1 per app)
  • If the right conditions do not exist then the agent will not be started.
     
    • Background agents run only in the situation whenever the operating system feels able to give them the processor to access.
     
  • If the phone gets into "Power Saver" mode then it may stop running the background agents completely
  • The user may not use agents to provide fundamental behaviors therefore:
     
    • A background agent is the cherry on top, not the ice-cream.

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:

  • Periodic tasks that run every now and then.
  • Resource Intensive tasks that run when the phone is in the position to let them.

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.

  • Runs every 30 mins.
  • Runs for 25 seconds or so.
  • Memory Usage allowed < 11 MB.
  • Agent is unscheduled after two consecutive crashes.
  • The phone sets a limit the maximum number of active agents at any time.
  • An agent is active after 2 weeks of starting (if the application is not used for two weeks then the operating system believes that you are not using this app and hence running its background is a waste of processing resources).

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:

  • It can run for up to 10 mins.
  • The phone must be on power source and the battery > 90% charged.
  • The phone must be connected to WiFi.
  • The phone should not be in use (the Lock screen is displayed).

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.


Similar Articles