Windows 10 IOT Lifecycle (or: how to property terminate a background application)

强颜欢笑 提交于 2019-12-04 02:49:10

You need to handle the canceled event. The background task will be canceled if the device is shutdown properly. Windows will also cancel tasks if they unregistered.

    BackgroundTaskDeferral _defferal;
    public void Run(IBackgroundTaskInstance taskInstance)
    {
         _defferal = taskInstance.GetDeferral();
        taskInstance.Canceled += TaskInstance_Canceled;
    }

    private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        //a few reasons that you may be interested in.
        switch (reason)
        {
            case BackgroundTaskCancellationReason.Abort:
                //app unregistered background task (amoung other reasons).
                break;
            case BackgroundTaskCancellationReason.Terminating:
                //system shutdown
                break;
            case BackgroundTaskCancellationReason.ConditionLoss:
                break;
            case BackgroundTaskCancellationReason.SystemPolicy:
                break;
        }
        _defferal.Complete();
    }

Cancellation Reasons

Canceled Event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!