Run back ground task in windows phone 7.5,
First. Create a Windows Phone Scheduled Task Agent project, name it for example: "BackGroundAgent". Open the default created ScheduledAgent.cs file. Write agent code in OnInvoke method:
protected override void OnInvoke(ScheduledTask task)
//TODO: Add code to perform your task in background
// If the Main App is Running, Toast will not show
ShellToast popupMessage = new ShellToast()
Title = "My First Agent",
Content = "Background Task Launched",
NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative)
Then, in the main Phone project's WMAppMainifest.xml, ExtendedTask node will be added automatically by VS 2010.
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
Specifier is the type of background agent. there are other two types: AudioPlayerAgent and AudioStreamingAgent. so, we must type it right. Name: give the name as you like, it is not referenced. Source: the assembly name of backagent project which contains the back agent code. here, the name of aseembly is "BackGroundAgent", we must type the right name. or windows phone application will not be able to find the assembly. Type: the type of backgroundagent.
Since the background agent existed as an independent project, From the main phone application, we need to add a dll reference to the BackGroundAgent.dll.
Next. Let's start background agent in the MainPage.xaml:
string taskName = "BackGroundTask";
// If the task exists
var oldTask = ScheduledActionService.Find(taskName);
if (oldTask != null)
PeriodicTask periodicTask = new PeriodicTask(taskName);
periodicTask.Description = "this save data to isolated Storage";
By default, Once background agent is enbled, it will run once every 30 minutes. the last line of code start the background agent 1 second after the phone app is closed, for test convenience. You may have a question in your mind now, How do we connect the phone application and back agent? I don't know either. what you need to do is: 1.check the WMAppMainifest.xml 2. specify a taskName and Description and add it to ScheduledActionService. Windows Phone will do all the magic.
Finally, it is time to test it. Press F5, press "Back" button to close the application, about 2 seconds later, A toast will show up with a pleasant ring bell.