窗10 IOT生命周期(或:如何物业终止后台应用程序) [英] Windows 10 IOT Lifecycle (or: how to property terminate a background application)

查看:413
本文介绍了窗10 IOT生命周期(或:如何物业终止后台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用上的无头树莓派2与Windows 10 IOT核心UWP应用,我们可以使用基本上只有一个在启动时执行的后台任务创建一个新的UWP应用程序的后台应用程序模板:

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 just a background task that is executed on startup:

<Extensions>
  <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundApplication1.StartupTask">
    <BackgroundTasks>
      <iot:Task Type="startup" />
    </BackgroundTasks>
  </Extension>
</Extensions>

为了保证应用程序的运行,我们可以使用下面的启动代码:

In order to keep an application running, we can use the following startup code:

public void Run( IBackgroundTaskInstance taskInstance )
{
  BackgroundTaskDeferral Deferral = taskInstance.GetDeferral();

  //Execute arbitrary code here.
}

这方法应用保持运行和之后的OS不会杀应用程序。在IOT宇宙中任何超时

This way the application keeps running and the OS won't kill the app after any timeout in the IOT universe.

到目前为止,如此之大

不过:我希望能够当设备关闭(或应用程序被要求'轻轻'接近正常关闭后台应用程序。

However: I want to be able to properly close the background application when the device shuts down (or the application is asked to 'gently' close.

在正常UWP应用程序,您可以订阅OnSuspending事件。结果
我怎样才能获得一个关于即将关闭通知/关闭在此背景情况?

In a 'normal' UWP application you can subscribe to the OnSuspending event.
How can I get a notification about an imminent shutdown / close in this background scenario?

帮助是极大的赞赏。结果
提前感谢!结果
-Simon

Help is greatly appreciated.
Thanks in advance!
-Simon

推荐答案

您需要处理的活动取消。 。在后台任务将设备是否正常关机被取消的Windows也将取消任务,如果他们注销

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();
    }

取消原因

Canceled事件

这篇关于窗10 IOT生命周期(或:如何物业终止后台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆