windows8 后台任务

后台任务示例
这个示例显示了如何创建并注册后台任务使用Windows运行后台任务API。
一个后台任务是由于系统或时间事件,可以由一个或多个条件的限制。当一个后台任务被触发,其关联处理程序运行和执行任务的工作背景。一个后台任务可以运行一个应用程序,即使注册了后台任务被暂停。
这个示例演示了以下工作:
•创建和注册后台任务用c#编写的或JavaScript。
•创建一个后台任务,由系统事件触发。
•添加一个条件,限制后台任务运行只有当条件实际上是。
•报告后台任务进度和完成到前台应用程序。
•处理取消后台任务。
•初始化任务进程以及背景完成处理程序在启动应用程序时。
  private  void OnProgress(IBackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs e)
        {
             //
            
//  Update the UI with progress reported by the background task.
            
//
            SampleDispatcher.InvokeAsync(CoreDispatcherPriority.Normal,
                                         (task, args) =>
                                         {
                                              var taskRegistration = task  as IBackgroundTaskRegistration;
                                              var progressArgs = args.Context  as BackgroundTaskProgressEventArgs;

                                              if ((task !=  null) && (args !=  null))
                                             {
                                                  switch (taskRegistration.Name)
                                                 {
                                                      case SampleBackgroundTaskName:
                                                         SampleBackgroundTaskProgress.Text =  " Progress:  " + progressArgs.Progress +  " % ";
                                                          break;
                                                      case SampleBackgroundTaskWithConditionName:
                                                         SampleBackgroundTaskWithConditionProgress.Text =  " Progress:  " + progressArgs.Progress +  " % ";
                                                          break;
                                                      case ServicingCompleteTaskName:
                                                         ServicingCompleteProgress.Text =  " Progress:  " + progressArgs.Progress +  " % ";
                                                          break;
                                                      case TimeTriggeredTaskName:
                                                         TimeTriggeredBackgroundTaskProgress.Text =  " Progress:  " + progressArgs.Progress +  " % ";
                                                          break;
                                                 }
                                             }
                                         },
                                         sender,
                                         e);

        } 

 完整示例 /Files/risk/windows8/后台任务sample.rar

你可能感兴趣的:(windows)