Jens Marchewka
Jens Marchewka

Reputation: 1443

Running Background Application on Windows IoT Core

I have created a BackgroundTask to run a WebService, however if i run my solution with debugger attached, everything works fine, slowly, but fine. But when i hit start in the appmanager (webinterface) it always says "failed to start package [MYPACKAGEID]". So what am i missing?

Here is the complete project: 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);
    }
}

Upvotes: 2

Views: 2873

Answers (2)

Rinsen
Rinsen

Reputation: 23

Edit: All these problems are gone with the latest version 10.0.14279.1000 and now the GUI finally works as it should.

I have been struggling with this to and have had great success with this method that might help someone. All is done in Power Shell

Put the device into headless mode, in some way I don´t think this is mandatory but I have not succeeded without it. Edit: This is not the case any more, it works as it should now.

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

Start the app in headless mode and add it to the startup app list To see what apps are in the startup list type

IotStartup startup

To add a headless app type in command

IotStartup add headless [Task1]

To add a headless app type in command

IotStartup startup headless [Task1]

To find the app name you can use the command

IotStartup list

To see that your app are in startup list type

IotStartup startup

Then reboot your device!

I have also had some problems related to removing apps from startup and then try to debug them via Visual Studio and in some cases the only solution were to flash the SD card with a new image.

For a complete list of available commands

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

Upvotes: 1

Jens Marchewka
Jens Marchewka

Reputation: 1443

The point is that there is no problem with the code or even the manifest, it seems that it's just not meant to run while the device is in "headed" mode, you need to set it as a satrtup headless app and then restart the device.

Upvotes: 3

Related Questions