0
我正在編寫一個UWP後臺應用程序,以使用物聯網的VS2017模板在Raspberry Pi上運行。該應用程序將監聽串行端口上的Arduino數據,因此我需要訪問異步方法。UWP後臺應用程序中的異步調用
我可以讓代碼編譯的唯一方法是使異步「Listen()」方法的返回類型爲void,但由於我無法等待Run()方法中對Listen()的調用它在到達FromIdAsync()時阻塞;
public async void Run(IBackgroundTaskInstance taskInstance)
{
// Create the deferral by requesting it from the task instance.
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
ListenAsync();
deferral.Complete();
}
private async void ListenAsync()
{
// some more code.....
serialPort = await SerialDevice.FromIdAsync(deviceID);
}
當試圖使返回類型Task, or Task<T>
我得到的編譯器錯誤:
Severity Code Description Project File Line Suppression State
Error Method 'RMSMPBackgroundApp.StartupTask.ListenAsync()' 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: ''. RMSMPBackgroundApp C:\Users\Dan.Young\documents\visual studio 2017\Projects\RMSMPBackgroundApp\RMSMPBackgroundApp\StartupTask.cs 41
我也試圖讓聽()方法作爲民營一些職位建議。
我對於異步編程相對比較陌生,所以我必須錯過某些明顯的東西?
謝謝。
謝謝,但這不適合我。我仍然收到錯誤,說明任務不是有效的Windows運行時類型。你能否提供包含run方法和'using'語句的整段代碼,以便我可以看到我是否缺少某些東西?謝謝。 –