如何正确使用任务?
How can I use Tasks correctly?
private async Task<List<PingReply>> PingAsync()
{
Ping pingSender = new Ping();
var tasks = serverNames.Select(ip => pingSender.SendPingAsync(ip, 2000));
var results = await Task.WhenAll(tasks);
return results.ToList();
}
我的问题是如何执行这个方法?
我试过了
List<string> data = PingAsync();
但我收到此错误消息
Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<System.Net.NetworkInformation.PingReply>>' to 'System.Collections.Generic.List<string>' ServerManager
我正在尝试 ping 服务器并更新 UI 以便我们可以监控服务器。
我也试过这些
Task<PingReply> longRunningTask = PingAsync();
PingReply result = await longRunningTask;
错误
Severity Code Description Project File Line Suppression State
Error CS4033 The 'await' operator can only be used within an async
method. Consider marking this method with the 'async' modifier and
changing its return type to 'Task'. ServerManager
Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type
'System.Threading.Tasks.Task>'
to
'System.Threading.Tasks.Task' ServerManager
查看您的 PingAsync
方法签名。 return 是什么意思? Task<List<PingReply>>
。添加 async
关键字不会改变该类型,假设它基本上允许您在方法内部使用 await
。
因此,通过调用 PingAsync
,您将获得类型为 Task<List<PingReply>>
的对象,并尝试将其分配给 List<string>
引用 - 这会导致类型不匹配错误。
但是,您可能会问,如何获得实际的 T
形式 Task<T>
?您应该简单地使用 await
,就像您在调用 Task.WhenAll
.
的方法中所做的那样
List<PingReply> data = await PingAsync();
错误信息本身就很清楚了。方法签名 return 是 Task<List<PingReply>>
,但出于某种原因您希望它是 return 是 List<string>
或 Task<PingReply>
,不知道为什么。
您应该注意到,在您的代码中开始使用 async
会使它像瘟疫一样传播开来。这意味着调用异步方法的更高级别的方法通常需要自己是 async
和 return a Task
或 Task<T>
.
给定这个 Async 函数
private async Task<List<PingReply>> PingAsync()
{
Ping pingSender = new Ping();
var tasks = serverNames.Select(
ip => pingSender.SendPingAsync(ip, 2000)
);
var results = await Task.WhenAll(tasks);
return results.ToList();
}
从对象的其他部分调用,接受 TASK 包装值
Task<List<PingReply>> foo = PingAsync();
然后手动等待...
foo.Wait();
然后提取值
List<PingReply> bar = foo.Result;
private async Task<List<PingReply>> PingAsync()
{
Ping pingSender = new Ping();
var tasks = serverNames.Select(ip => pingSender.SendPingAsync(ip, 2000));
var results = await Task.WhenAll(tasks);
return results.ToList();
}
我的问题是如何执行这个方法?
我试过了
List<string> data = PingAsync();
但我收到此错误消息
Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<System.Net.NetworkInformation.PingReply>>' to 'System.Collections.Generic.List<string>' ServerManager
我正在尝试 ping 服务器并更新 UI 以便我们可以监控服务器。
我也试过这些
Task<PingReply> longRunningTask = PingAsync();
PingReply result = await longRunningTask;
错误
Severity Code Description Project File Line Suppression State Error CS4033 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. ServerManager
Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Threading.Tasks.Task' ServerManager
查看您的 PingAsync
方法签名。 return 是什么意思? Task<List<PingReply>>
。添加 async
关键字不会改变该类型,假设它基本上允许您在方法内部使用 await
。
因此,通过调用 PingAsync
,您将获得类型为 Task<List<PingReply>>
的对象,并尝试将其分配给 List<string>
引用 - 这会导致类型不匹配错误。
但是,您可能会问,如何获得实际的 T
形式 Task<T>
?您应该简单地使用 await
,就像您在调用 Task.WhenAll
.
List<PingReply> data = await PingAsync();
错误信息本身就很清楚了。方法签名 return 是 Task<List<PingReply>>
,但出于某种原因您希望它是 return 是 List<string>
或 Task<PingReply>
,不知道为什么。
您应该注意到,在您的代码中开始使用 async
会使它像瘟疫一样传播开来。这意味着调用异步方法的更高级别的方法通常需要自己是 async
和 return a Task
或 Task<T>
.
给定这个 Async 函数
private async Task<List<PingReply>> PingAsync()
{
Ping pingSender = new Ping();
var tasks = serverNames.Select(
ip => pingSender.SendPingAsync(ip, 2000)
);
var results = await Task.WhenAll(tasks);
return results.ToList();
}
从对象的其他部分调用,接受 TASK 包装值
Task<List<PingReply>> foo = PingAsync();
然后手动等待...
foo.Wait();
然后提取值
List<PingReply> bar = foo.Result;