我似乎无法理解如何结构化异步调用SendPingAsync。目前我想循环遍历一个IP地址列表并在程序继续之前对它们进行异步ping...现在一次只能一个地去ping所有的地址,这需要很长时间。我之前问过一个关于这个问题的问题,以为自己可以弄懂异步但显然我错了。
private void button1_Click(object sender, EventArgs e)
{
this.PingLoop();
MessageBox.Show("hi"); //for testing
}
public async void PingLoop()
{
Task<int> longRunningTask = PingAsync();
int result = await longRunningTask;
MessageBox.Show("async call is finished!");
//eventually want to loop here but for now just want to understand how this works
}
private async Task<int> PingAsync()
{
Ping pingSender = new Ping();
string reply = pingSender.SendPingAsync("www.google.com", 2000).ToString();
pingReplies.Add(reply); //what should i be awaiting here??
return 1;
}
我恐怕对这里到底发生了什么不是很清楚...在什么情况下应该返回任务?当我按照原样运行时,界面会冻结并出现ping错误。我阅读了MSDN文档和大量的问题,但仍然无法理解。