简体   繁体   中英

How to configure headless Windows IoT application project to support async/await?

This sample shows how to implement a headless app, which worked for me on Raspberry Pi.

This sample shows an app that uses Task and async/await. It also runs on Raspberry Pi with zero problem.

However, when I add a function with async in it's signature and the return type of Task to the headless app project like that:

public async Task Test() 
{
}

I'm getting compilation error that informs me that it is not supported on Windows Runtime:

error WME1038: Method 'BlinkyHeadlessCS.ReadMessagesFromDevice.ChangeDeviceMode()' has a parameter of type 'System.Threading.Tasks.Task' in its signature. Although this type is not a valid Windows Runtime type, it implements interfaces that are valid Windows Runtime types. Consider changing the method signature to use one of the following types instead: ''.

From this message I make a conclusion that the reason that it does not compile is that the project output type is WinRT. But I'd like to know how to configure the project correctly so it acts similar to what the headed example does ?.

Specifically, I'd like the function in the example above to successfully compile and work as it does if added to the headed sample.


What I tried so far :

  1. Changed the project output to the only other option available in the dropdown: "Class Library". Failed compilation with this error:

error : One of your dependencies requires the .NET Framework, but the .NET Framework could not be found in the NuGet packages installed in this project. Please install the appropriate .NET Framework packages required by your dependency.

I'm not sure what "appropriate nuget packages" this error message could possibly refer to. I'm also not sure if "Class Library" is the correct output type.

  1. Tried to remove the UI from the second example. Failed compilation with the following error:

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

  1. Tried to add add an empty Main in the app. Deployment failed with the following error:

Error DEP0001 : Unexpected Error: -2145615869

Run the following.

Install-Package Nito.AsyncEx

Then do the following in your code

public static class Program
{
    public int static Main()
    {
        AsyncContext.Run(MainImpl);
    }

    private async Task MainImpl()
    {
        //Do stuff.
    }
}

在方法中使用内部修饰符而不是公共

I don't use WinRT, but I'm pretty sure you aren't supposed to use Tasks in it.

From this blog:

https://blogs.msdn.microsoft.com/windowsappdev/2012/06/14/exposing-net-tasks-as-winrt-asynchronous-operations/

Looks like for async await in WinRT, you need to translate your return types:

void => AsyncActionCompletedHandler
Task => AsyncActionWithProgressCompletedHandler<TProgress>
Task<TResult> => AsyncOperationWithProgressCompletedHandler<TResult, TProgress>

The common interface implemented by Task and the WinRT equivalents is IASyncResult, as you might expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM