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

前端 未结 1 2161
清歌不尽
清歌不尽 2021-02-20 15:38

In order to use a UWP application on a headless Raspberry Pi 2 with Windows 10 IOT Core we can use the background application template which basically creates a new UWP app with

1条回答
  •  一整个雨季
    2021-02-20 16:03

    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

    0 讨论(0)
提交回复
热议问题