In this article I will talk about background agents in Windows Phone. Background agents and scheduled tasks (PeriodicTask and ResourceIntensiveTask) executes the application in background.
Let's write code:
Step 1: Create Windows Phone Scheduled Task Agent.

Step 2: Create Silverlight for Windows Phone project.
Step 3: Add two buttons in ContentPanel of MainPage.xaml. One to schedule periodic task and other is to schedule resource intensive task.
<Button x:Name="SchedulePeriodic" Margin="20,340,0,0" Height="80" Width="430" Click="SchedulePeriodic_Click" Content="Schedule Periodic Task" />
<Button x:Name="ScheduleResourceIntensive" Margin="20,480,0,0" Height="80" Width="430" Click="ScheduleResourceIntensive_Click" Content="Schedule Resource Intensive Task" />
There are two types of scheduled tasks.
1. PeriodicTask: Periodic tasks run for small amount of time.
2. ResourceIntensiveTask: Resource intensive task which runs relatively longer than periodic task.
Step 4: Add below method in MainPage.xaml.cs which will start scheduled PeriodicTask.
Description needs to be added, if ignored ArgumentOutOfRangeException will be thrown.
private void SchedulePeriodic_Click(object sender, RoutedEventArgs e) { PeriodicTask periodicTask; string periodicTaskName = "ScheduledPeriodicTask";
periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask; if (periodicTask != null) { Remove(periodicTaskName); }
periodicTask = new PeriodicTask(periodicTaskName); periodicTask.Description = "This demonstrates a periodic task."; try { ScheduledActionService.Add(periodicTask);
#if(DEBUG_AGENT) ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(60)); #endif } 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."); } 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. } } catch (SchedulerServiceException) { // No user action required. } }
Step 5: Add below method in MainPage.xaml.cs to schedule Resource intensive task.
private void ScheduleResourceIntensive_Click(object sender, RoutedEventArgs e) { ResourceIntensiveTask resourceIntensiveTask; string resourceIntensiveTaskName = "ScheduleResourceIntensive";
resourceIntensiveTask = ScheduledActionService.Find(resourceIntensiveTaskName) as ResourceIntensiveTask; if (resourceIntensiveTask != null) { Remove(resourceIntensiveTaskName); }
resourceIntensiveTask = new ResourceIntensiveTask(resourceIntensiveTaskName);
try { ScheduledActionService.Add(resourceIntensiveTask);
// If debugging is enabled, use LaunchForTest to launch the agent in one minute. #if(DEBUG_AGENT) ScheduledActionService.LaunchForTest(resourceIntensiveTaskName, TimeSpan.FromSeconds(60)); #endif } 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."); } 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. } } catch (SchedulerServiceException) { // No user action required. } }
Step 6: Add below Remove method in MainPage.xaml.cs to remove task from ScheduledActionService.
private static void Remove(string taskName) { try { ScheduledActionService.Remove(taskName); } catch (Exception) { } }
Step 7: Now open ScheduledAgent.cs file of ScheduledTaskAgent project. Modify OnInvoke method like below. You can find out the type of scheduled task and take respective action. One can schedule both periodic and resource intensive task in same appplication.
Last statement of below method "NotifyComplete" informs operating system that the agent has completed its intended task for the current invocation of the agent.
protected override void OnInvoke(ScheduledTask task) { string toastTitle = "";
if (task is PeriodicTask) { toastTitle = "Periodic "; } else { toastTitle = "Resource-intensive "; }
ShellToast toast = new ShellToast(); toast.Title = toastTitle; toast.Content = "Background Agents notification....."; toast.Show();
#if DEBUG_AGENT ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60)); #endif NotifyComplete(); }
Step 8: Add below line as first statement of MainPage.Xaml.cs and ScheduldedAgent.cs files to enable debugging.
#define DEBUG_AGENT
Step 9: Now run the application and you will get screen like below with two buttons.

Step 10: Click on Schedule Periodic Task button. You will get periodic task notification like below.

Step 11: Click on Schedule Resource Intensive Task button. You will get resource intensive task notification like below.

Constraints of Scheduled Tasks
1. Unsupported APIs
2. Memory usage cap
3. Reschedule required every two weeks
4. Agents (Background Agents) unscheduled after two consecutive crashes
Constraints of Periodic Agents
1. Scheduled interval: 30 minutes
2. Scheduled duration: 25 seconds
3. Battery Saver mode can prevent execution
4. Per-device periodic agent limit
Constraints of Resource-intensive Agents
1. Duration: 10 minutes
2. External power required
3. Non-cellular connection required
4. Minimum battery power
5. Device screen lock required
6. No active phone call
7. Cannot change network to cellular
Note : Background agents are not supported in 256 MB. Read How to: Disable Features of an Application for a 256-MB Device
This ends the article of Background Agents in Windows Phone.
|