gpt4 book ai didi

c# - 无法将类型 'System.Threading.Tasks.Task' 隐式转换为 'Windows.Foundation.IAsyncAction' 。存在显式转换

转载 作者:行者123 更新时间:2023-11-30 23:17:11 24 4
gpt4 key购买 nike

所以我用 C# 编写这个软件,但收到了此错误消息。对于我的一生,我无法弄清楚,无论我做什么,搜索都不会带来任何有用的东西。

代码本身:

private async Task LoginIoTHub()
{
TypeJson command_json = new TypeJson();

if (NowNetSta == false) return;
if (IoTHubConnectFlag == false) return;

if (TeamName == null)
{
Debug.WriteLine("Please don't let your team name be void when you call methods 'new IoTHubLocalService(xx,xx,xx)'");
await Task.Delay(TimeSpan.FromSeconds(4));
}
else
{
if (LoginF == false)
{
try
{
Debug.WriteLine("Start to login\r\n");

LoginJson login_msg = new LoginJson();
login_msg.DeviceID = BoardID;
login_msg.name = TeamName;

var messageString = JsonConvert.SerializeObject(login_msg);

command_json.Type = 1;
command_json.Command = messageString;
messageString = JsonConvert.SerializeObject(command_json);

var message = new Message(Encoding.ASCII.GetBytes(messageString));
IAsyncAction Action = deviceClient.SendEventAsync(message);

await Action;

if (Action.Status == AsyncStatus.Completed)
{
Debug.WriteLine("Login success");
LoginF = true;
}
else
{
Debug.WriteLine("Login failed");
await Task.Delay(TimeSpan.FromSeconds(10));
}
}

catch (Exception ex)
{
Debug.WriteLine("Login to IoT Hub failed, please check your team name and internet connection: " + ex.Message);
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
}
}

最佳答案

DeviceClient.SendEventAsync(Message)返回 Task

Sends an event to device hub

public Task SendEventAsync(Message message)

将代码更改为

var Action = deviceClient.SendEventAsync(message);
await Action;

另请注意,Task 继承自 IAsyncResult,而不是像示例中那样继承自 IAsyncAction

您还可以使用 Task.IsCompleted Property 检查完成情况

if (Action.IsCompleted) { ...

关于c# - 无法将类型 'System.Threading.Tasks.Task' 隐式转换为 'Windows.Foundation.IAsyncAction' 。存在显式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com