在 Windows IoT Core 上运行后台应用程序

本文关键字:运行 后台 应用程序 Core Windows IoT | 更新日期: 2023-09-27 18:37:12

我创建了一个 BackgroundTask 来运行 Web 服务,但是如果我在附加调试器的情况下运行我的解决方案,一切都很好,很慢,但很好。但是当我在应用程序管理器(网络界面)中点击开始时,它总是说"无法启动包[MYPACKAGEID]"。那么我错过了什么?

以下是完整的项目: https://github.com/naice/HomeAutomation.git

public sealed class StartupTask : IBackgroundTask
{
    internal static BackgroundTaskDeferral Deferral = null;
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        // 
        // TODO: Insert code to perform background work
        //
        // If you start any asynchronous methods here, prevent the task
        // from closing prematurely by using BackgroundTaskDeferral as
        // described in http://aka.ms/backgroundtaskdeferral
        //
        Deferral = taskInstance.GetDeferral();
        await ThreadPool.RunAsync(async workItem => {
            RestWebServer restWebServer = new RestWebServer(80);
            try
            {
                // initialize webserver
                restWebServer.RegisterController<Controller.Home.Home>();
                restWebServer.RegisterController<Controller.PhilipsHUE.Main>();
                await restWebServer.StartServerAsync();
            }
            catch (Exception ex)
            {
                Log.e(ex);
                restWebServer.StopServer();
                Deferral.Complete();
            }
        }, WorkItemPriority.High);
    }
}

在 Windows IoT Core 上运行后台应用程序

关键是代码甚至清单都没有问题,似乎它只是不打算在设备处于"headed"模式时运行,您需要将其设置为satrtup无头应用程序,然后重新启动设备。

编辑:最新版本 10.0.14279.1000 消除了所有这些问题,现在 GUI 终于正常工作了。

我一直在为此苦苦挣扎,并且使用这种方法取得了巨大的成功,这可能会帮助某人。一切都在Power Shell中完成

将设备置于无头模式,在某种程度上,我认为这不是强制性的,但没有它我就没有成功。编辑:情况不再如此,它现在应该工作。

https://ms-iot.github.io/content/en-US/win10/HeadlessMode.htm

以无外设模式启动应用并将其添加到启动应用列表查看启动列表类型中的应用

IotStartup startup

在命令中添加无外设应用类型

IotStartup add headless [Task1]

在命令中添加无外设应用类型

IotStartup startup headless [Task1]

要查找应用程序名称,您可以使用以下命令

IotStartup list

查看应用是否在启动列表类型中

IotStartup startup

然后重新启动您的设备!

我也遇到了一些与从启动中删除应用程序有关的问题,然后尝试通过Visual Studio调试它们,在某些情况下,唯一的解决方案是使用新映像刷新SD卡。

有关可用命令的完整列表

https://ms-iot.github.io/content/en-US/win10/tools/CommandLineUtils.htm