Windows Phone Background Agents

By | August 12, 2013

Introduction

Background agents are the scheduled tasks which run in background and add value to your application which the other application might not have. Here we will cover all 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 the background agents, background agents keep the user in touch with the application when the application is not actually working(i.e. not working in foreground). We will look at the different ways in which we can use the background agents. Windows Phone has a 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 ‘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 a philosophy – “users first”, so the application running in the foreground is the only application, there might be other applications which have lots of memory but none of CPU, that is the reason Windows Phone has the most fluid interface. Now here comes in role the background agents, as we mentioned the app in foreground is the only app running but we also need some background processing for other apps like fetching emails etc. So instead of running the complete application in background we have background agents which run on behalf of the application and performs some 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 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 agent can never be compared with the foreground application running in background as it has certain limitations. A background agents is limited in what it can do and access it has to the processor and other phone facilities.

Background Agent Health Warning

Operating System manages all the background agents, you can go to settings and manage all your running background agents,

  • The number of background agents is limited per application (i.e. 1 per app)
  • If the right conditions do not arise 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 it may stop running background agents completely
  • User may not use agents to provide fundamental behaviors
    • A background agents 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 means that it can just add the value to your application and not provide the basic features, they are kept as light as possible. Operating System manages all the background agents so as to ensure the behavior of  each agent and hence no agents can behave abnormal to utilize more CPU or memory to disturb smooth execution of the applications.

Agents and Tasks

Basically Task is the scheduling type you request when you schedule a background agent to run, and it is managed by the operating system which 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 loading on the phone. It is intended to perform a task regularly and complete quickly. It is good for location tracking, polling background services, tiles update.

  • 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, operating system believes that you are not using this app and hence running its background is waste or processing resources.)

ResourceIntensive Agents

These are the agents which require high resources to perform the task, usually involves updating of phone, downloading large files etc. Whenever processor feels it is able to give access to the application the agents start running. Few features of ResourceIntensive 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(Lock screen displayed).

Dual purpose Agents

There are some dual purpose agents also, as the name signifies 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 behave accordingly. The agent will run periodically and when the phone is in 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 which 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 the coming posts we will learn how to implement toast notifications in your application.

One thought on “Windows Phone Background Agents

  1. τουλάχιστον

    I think the admin of this web site is really working hard in support
    of his website, for the reason that here every information is quality
    based stuff.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *