我似乎无法理解如何构建对SendPingAsync的异步调用.我想循环遍历一个IP地址列表,并在继续执行程序之前异步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;
}
Run Code Online (Sandbox Code Playgroud)
我恐怕只是不知道这里到底发生了什么......我应该什么时候回来?当我按原样运行时,我只是得到一个冻结的UI和一个ping错误.我在这里阅读了MSDN文档和大量问题,我只是没有得到它.