none
当trigger触发之后,后台任务并没有执行,而是整个应用程序退出了,麻烦大家来看看 RRS feed

  • 问题

  • 下面是我的代码

    后台任务注册代码:

     
     private void RegisterBackgroundTask(object sender, RoutedEventArgs e)
            {
                try
                {
                    var task = BackgroundRegister.RegisterBackgroundTask(BackgroundRegister.SampleBackgroundTaskEntryPoint,
                                                                           BackgroundRegister.SampleBackgroundTaskName,
                                                                           new SystemTrigger(SystemTriggerType.TimeZoneChange, false),
                                                                           null);
                    AttachProgressAndCompletedHandlers(task);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

    后台任务注册类:

    class BackgroundRegister
        {
            public const string SampleBackgroundTaskEntryPoint = "ReceiveTask.ReciveBackgroundTask";
            public const string SampleBackgroundTaskName = "ReciveBackgroundTask";
    
            public static BackgroundTaskRegistration RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
            {
                var builder = new BackgroundTaskBuilder();
    
                builder.Name = name;
                builder.TaskEntryPoint = taskEntryPoint;
                builder.SetTrigger(trigger);
    
                if (condition != null)
                {
                    builder.AddCondition(condition);
                }
    
                BackgroundTaskRegistration task = builder.Register();
    
                return task;
            }
        }


    后台任务执行代码:

    public sealed class ReciveBackgroundTask : IBackgroundTask
        {
            public async void Run(IBackgroundTaskInstance taskInstance)
            {
                // 后台任务在执行中被终止执行时所触发的事件
                taskInstance.Canceled += taskInstance_Canceled;
    
                // 异步操作,即通知系统后台任务可在 IBackgroundTask.Run 方法返回后继续工作
                BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
    
                try
                {
                    // 指定后台任务的进度
                    taskInstance.Progress = 0;
    
                    // 写入相关数据到文件
                    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdBackgroundTask\demo.txt", CreationCollisionOption.ReplaceExisting);
                    await FileIO.WriteTextAsync(file, "progress: 0, currentTime: " + DateTime.Now.ToString());
    
                    for (uint progress = 10; progress <= 100; progress += 10)
                    {
                        await Task.Delay(1000);
    
                        // 更新后台任务的进度
                        taskInstance.Progress = progress;
    
                        // 写入相关数据到文件
                        file = await ApplicationData.Current.LocalFolder.GetFileAsync(@"webabcdBackgroundTask\demo.txt");
                        await FileIO.AppendTextAsync(file, "progress: " + progress.ToString() + ", currentTime: " + DateTime.Now.ToString());
                    }
                }
                finally
                {
                    // 完成异步操作
                    deferral.Complete();
                }
            }
    
            void taskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
            {
                /*
                 * BackgroundTaskCancellationReason - 后台任务在执行中被终止执行的原因
                 *     Abort - 前台 app 调用了 IBackgroundTaskRegistration.Unregister(true)
                 *     Terminating - 因为系统策略,而被终止
                 *     LoggingOff - 因为用户注销系统而被取消
                 *     ServicingUpdate - 因为 app 更新而被取消
                 */
            }
        }

    我也在声明里写上了入口点如下

    麻烦大家看看为什么我触发时区更改的trigger整个应用程序就退出呢?

    之前我觉得是系统中已经存在同名的后台任务,于是我改了名字改了入口点,但还是一出发trigger整个应用程序还是退出,run方法都不进

    2013年7月2日 7:41

答案