Using Background Agents in your Windows Phone 7.5 (Mango) Apps


Windows Phone 7.5, codenamed Mango, added around 500 new features over the first version of Windows Phone platform. One of the features is the ability to schedule tasks and execute them through background agents. They can execute even when the application is not running in the foreground.

There are two types of scheduled tasks one can use in a Windows Phone application.

  1. Periodic agents are tasks that run at regular intervals for a small amount of time. An example would be periodic data synchronization done by some social application like Facebook. This is implemented by PeriodicTask class.
  2. Resource-intensive agents are tasks that run for a longer period of time, (like weekly application updates being downloaded in the background). This is implemented by ResourceIntensiveTask class.

Before using Background agents in their application, one needs to understand that certain APIs are not allowed to be called from a background agent. APIs for Camera, VibrateController, Radio, BackgroundAudioPlayer, Sensors, etc. are not allowed to be back from a background agent, and hence cannot be executed by a scheduled task.

The most used APIs in a scheduled task are of type ShellToast and HttpWebRequest.


Create a Windows Phone project in Visual Studio. Add a new project to this solution of the type "Windows Phone Scheduled Task Agent" called WindowsPhoneScheduledTaskAgent.

Open up the ScheduledAgent.cs file that is auto-generated by the Visual Studio Editor.

In the OnInvoke Method, add the following code.

protected override void OnInvoke(ScheduledTask task)
            //TODO: Add code to perform your task in background
            ShellToast backgroundToast = new ShellToast();
            backgroundToast.Title = "Scheduled Task";
            backgroundToast.Content = "Running...";
            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(10));

Now, in our main project file, add a button control (called buttonAddPeriodicTask) to the MainPage.xaml. Add a PeriodicTask variable to the class.

PeriodicTask myPeriodicTask;

Now In the buttonAddPeriodicTask's click event, add the following code

private void buttonAddPeriodicTask_Click(object sender, RoutedEventArgs e)
            myPeriodicTask = ScheduledActionService.Find("My periodic task") as PeriodicTask;
            if (myPeriodicTask != null)
                ScheduledActionService.Remove("My periodic task");
            myPeriodicTask = new PeriodicTask("My periodic task");
            myPeriodicTask.Description = "This demonstrates a periodic task.";

            stackPanel1.DataContext = myPeriodicTask;
            ScheduledActionService.LaunchForTest("My periodic task", TimeSpan.FromSeconds(10));

Compile and execute the project. If you are having issues compiling, you can download a working sample of code from here.

Click the
Figure 1: Click the "Add periodic task" button

Click the "Add periodic task" button and then in a couple of seconds, click the "Windows" button to go to the home screen.

You will notice that after around 20 seconds, you will notice a notification at the topic of the screen as displayed below.

Notification from the scheduled task
Figure 2: Notification from the scheduled task

This is the notification from the scheduled task that appeared, indicating that the scheduled task has run.

If you are using PeriodicTask, as in our demo, you should see the notification appear every 20 seconds until the phone is rebooted or the emulator is shutdown.


In this article, we learned how to use background agents to run scheduled tasks in Windows Phone 7.5 applications. I hope you have found this information useful.

About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at

Related Articles



  • the notification appear every 20 seconds

    Posted by likewer on 04/01/2013 06:17pm

    i testing ady, but the notification will not appear in every 20 seconds. what wrong?

  • dependency and reference

    Posted by mauro on 05/30/2012 01:03am

    please, specify that you need to add dependency from main project to agent and add reference to main project to agent

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds