Background Task (background tasks) Periodictask (cycle tasks) and resourceintensivetask (resource-intensive tasks)
Introduced
Unique Windows Phone 7.5 (SDK 7.1) after the task
Periodictask-Cycle Tasks
Resourceintensivetask-resource-intensive tasks (usually used for mobile phones and computers to synchronize data)
Example
Demonstrates how to register and run Periodictask types of tasks and resourceintensivetask types of tasks
1. Background Agent
Myscheduledtaskagent/scheduledagent.cs
* * This example shows how to periodically eject Toast from a background task, and how to update the application tile Badge * It is recommended to create this project using scheduledtaskagent type templates * * backgroundagent-background generation class, abstract class, which is the base class of Scheduledtaskagent, Audioplayeragent, and Audiostreamingagent * Notifycomplete ()-used to inform the system that the agent has completed the current task, calling this method, the system will be ready to perform the next task * Abort ()-To notify the system, discard this and future tasks, the corresponding scheduledaction isscheduled will be false * * * Scheduledt Askagent-Background Scheduled Task proxy class, abstract class * Oninvoke (Scheduledtask Task)-The background task calls this method every time it executes * * scheduledagent-class that is used to demonstrate background tasks, Inherited from Scheduledtaskagent, you need to rewrite the scheduledtaskagent oninvoke () method * * Devicestatus.applicationmemoryusagelimit-Program in The amount of memory allocated at this time (in bytes) may not be the same each time you get this value, but certainly not more than 6MB * * shelltoast-for managing Toast * Title-toast Title * Content- Toast content * Navigationuri-click the destination address (Uri type) that is linked to after Toast * Show ()-Display Toast (note: If the calling program for the show method is running in the foreground, it will not be displayed Toas T) * * Scheduledactionservice.launchfortest (string name, TimeSpan delay) * For Scheduledtask type of task, immediately after the specified time Perform the task. Applications for development purposes only in deployment of development toolsThe effective * Periodictask and Resourceintensivetask in the program are inherited from Scheduledtask * * using System.Windows;
Using Microsoft.Phone.Scheduler;
Using Microsoft.Phone.Shell;
Using Microsoft.Phone.Info;
Using System;
Using System.Linq; Namespace Myscheduledtaskagent {public class Scheduledagent:scheduledtaskagent {* * * _CL Assinitialized-Used to mark whether the scheduledagent has been initialized * marked as volatile to avoid the compiler's belief that this field is not externally modified, and that it is optimized into registers (fields marked as volatile are only Put in memory) * Generally speaking, fields shared between tasks in a multitasking environment should be marked as volatile/private static volatile bool _classinitia
lized; Public Scheduledagent () {if (!_classinitialized) {_classinitialized
= true; Deployment.Current.Dispatcher.BeginInvoke (Delegate {Application.Current.UnhandledEx
Ception + = scheduledagent_unhandledexception;
}); }} Private void Scheduledagent_unhandledexception (object sender, Applicationunhandledexceptioneventargs e) {
if (System.Diagnostics.Debugger.IsAttached) {System.Diagnostics.Debugger.Break (); }} protected override void Oninvoke (Scheduledtask Task) {string to
Asttitle = "";
if (task is periodictask) Toasttitle = "Periodictask";
else if (task is resourceintensivetask) Toasttitle = "Resourceintensivetask";
String toastcontent = "Memoryusagelimit:" + devicestatus.applicationmemoryusagelimit;
Pop-up Toast shelltoast Toast = new Shelltoast (); Toast.
Title = Toasttitle; Toast.
Content = toastcontent; Toast.
Navigationuri = new Uri ("/backgroundtask/backgroundagentdemo.xaml?param=abc¶m2=xyz", UriKind.Relative); Toast. Show ();
Update the application tile Badge shelltile applicationtile = ShellTile.ActiveTiles.First (); Standardtiledata newtile = new Standardtiledata {Count = new Random ().
Next (1, 99)};
Applicationtile.update (Newtile); #if DEBUG//15 seconds to execute task. Name task Scheduledactionservice.launchfortest.
Name, Timespan.fromseconds (15));
#endif notifycomplete (); When the main program references this project, the following information is added to the manifest: * <extendedtask name= "Backgroundtask" > * <backgroundse Rviceagent specifier= "scheduledtaskagent" name= "myscheduledtaskagent" source= "Myscheduledtaskagent" Myscheduledtaskagent.scheduledagent "/> * </ExtendedTask> *
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/