3

我的问题是,该deferral.complete()方法到底是什么,该方法是否调用事件task.Compledet,或者有没有办法从BackgroundTaskSyncer我的类中调用方法BackgroundSyncer????当我运行程序时,我会从 BackgroundTaskSyncer 执行 Run 方法,但在其他类中什么也不做?

    namespace NotificationTask
            {
                public sealed class BackgroundTaskSyncer : IBackgroundTask
                {
                    public void Run(IBackgroundTaskInstance taskInstance)
                    {
                        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
                        deferral.Complete();
                    }
                }
                }
    namespace Services
    {

    public static class BackgroundSync
        {
    private static async Task RegisterBackgroundTask()
            {
                try
                {
                    BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
                    if (status == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity || status == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
                    {
                        bool isRegistered = BackgroundTaskRegistration.AllTasks.Any(x => x.Value.Name == "Notification task");
                        if (!isRegistered)
                        {
                            BackgroundTaskBuilder builder = new BackgroundTaskBuilder
                            {
                                Name = "Notification task",
                                TaskEntryPoint =
                                    "NotificationTask.BackgroundTaskSyncer"
                            };
                            builder.SetTrigger(new TimeTrigger(15, false));
                            builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
                            BackgroundTaskRegistration task = builder.Register();
                            task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
                            task.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("The access has already been granted");
                }
            }
    private static void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
            {
                ToTheBackGroundWork();
            }
4

1 回答 1

6

Deferrals were created to work around a problem with async void events and methods. For example, if you had to await during a background operation, you would use an async void Run method. But the problem with that is that the runtime has no idea that you actually have more work you want to do.

So, a deferral is an object that you can use to inform the runtime "I'm really done now." A deferral is only necessary if you need to await.

I have a blog post that goes into "asynchronous event handlers" and deferrals in more detail.

于 2013-07-03T15:29:27.717 回答