How to create simple Background Agent in Windows Phone
Delete instructional text in italic
This article explains how to ... Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.
Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use
This article explains how to create a simple background agent in Windows Phone. There is a MSDN example on how to create a background agent but its not simple it has additional stuff in it, so if you are looking for just plain simple how to ? here it is.
You need to create an app project , it can be anything from simple basic windows phone app to panorama or pivote based application. Then create your background agent project in the same solution and add its reference to the main app, with necessary code ( which is hardly few lines) you can have your background agent running. You can test agents in simulator as well.
- Create a simple windows phone application project as follows and give some suitable name to it.
- Create a scheduled task agent project in Visual Studio, this should be added to the same solution , an easy way to do is, goto solution explorer of main application right click on solution and select add, select add project and automatically the project created will get added to the existing solution. Assign some suitable name to the project.
- Next step is to add a reference to the main application it can be done by going to Solution Explorer, right click references under main project and select Add Reference option.
- Next select Solutions section on the Add Reference dialog and you should be able to see the name of your scheduled task agent project name there select it and its done.
- Next open ScheduledAgent.cs file from your background agent project and add following lines of code in OnInvoke method
protected override void OnInvoke(ScheduledTask task)
string toastMessage = "Periodic task running.";
ShellToast toast = new ShellToast();
toast.Title = "Background Agent Sample";
toast.Content = toastMessage;
Once the above code is added the Scheduled Agent or your background task is ready to fire a Toast notification whenever its called. As you may tun this app to test , dont forget to add following macro in at the top in your ScheduledAgent.cs file
and after the above macro make sure you have all following namespace imports in place
- Now next step is to setup this Agent (task) to run in background from your main app , it can be done with the help of following lines of code. Make sure you place them in your MainPage.xaml.cs file of main project, I am attaching the complete file for your exposure to the code.
public partial class MainPage : PhoneApplicationPage
string periodicTaskName = "PeriodicAgent";
// Sample code to localize the ApplicationBar
agentsAreEnabled = true;
// Obtain a reference to the period task, if one exists
periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
if (periodicTask != null)
periodicTask = new PeriodicTask(periodicTaskName);
// The description is required for periodic agents. This is the string that the user
// will see in the background services Settings page on the device.
periodicTask.Description = "This demonstrates a periodic task.";
// Place the call to Add in a try block in case the user has disabled agents.
// If debugging is enabled, use LaunchForTest to launch the agent in one minute.
catch (InvalidOperationException exception)
if (exception.Message.Contains("BNS Error: The action is disabled"))
MessageBox.Show("Background agents for this application have been disabled by the user.");
agentsAreEnabled = false;
if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
// No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
// No user action required.
private void RemoveAgent(string name)
We call LaunchForTest function only for debugging , which should be remove once your application goes into production environment. Make sure to remove the DEBUG macro once you are done debugging of the project which will stop calling the agent after every few seconds , which should be done only if you wish to debug the application. If you keep that line as it is it may cause failure in app submission process.
Remove Category:Draft when the page is complete or near complete
The "platform categories" will be displayed here in preview only - Copy paste relevant categories into text here
Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]
Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]
Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]
Add categories below using category selector.