异步编程试图了解等待如何工作

时间:2018-09-15 10:51:40

标签: c# asynchronous async-await

因此,我正在学习一些基本的异步编程,并遵循本教程: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/但是,我得到的输出与期望的有所不同。

这是我的代码:

private async void btn1_Click(object sender, EventArgs e)
{
   await TestAsync();
   Console.WriteLine("terminate");
}

private async Task TestAsync()
{
   string str = await Todo();
   await Task.Delay(500);  //added await operator as per FCin advice
   Console.WriteLine(str);
}

private async Task<string> Todo()
{
   await Task.Delay(3000); //added await operator as per FCin advice
   return "return from async task";
}

从单击btn1开始,将触发btn1_Click方法。 首先,它将调用TestAsync()。 由于TestAsync方法的第一行是等待异步方法,因此我的理解是,在这一点上,等待操作员应暂停TestAsync并将控制权返回给TestAsync的调用者btn1_Click。 这应打印“终止”,然后程序应耐心等待TestAsync完成,并最终打印“从异步任务返回”。 但是,我得到的输出是反向顺序,因此我试图理解原因。

因此,我对其进行了修改,以便现在可以在btn1_Click方法中等待TestAsync。另外,我将线程休眠更改为任务延迟,但是我仍然得到相同的输出...

Edit2:我作为示例使用的代码

// 1. Three things to note in the signature:  
//  - The method has an async modifier.   
//  - The return type is Task or Task<T>. (See "Return Types" section.)  
//    Here, it is Task<int> because the return statement returns an integer.  
//  - The method name ends in "Async."  
async Task<int> AccessTheWebAsync()  
{   
// 2. You need to add a reference to System.Net.Http to declare client.  
HttpClient client = new HttpClient();  

// 3. GetStringAsync returns a Task<string>. That means that when you await 
// the task you'll get a string (urlContents).  
Task<string> getStringTask = 
client.GetStringAsync("http://msdn.microsoft.com");  

// 4 .You can do work here that doesn't rely on the string from 
//GetStringAsync.  
DoIndependentWork();  

// 5. The await operator suspends AccessTheWebAsync.  
//  - AccessTheWebAsync can't continue until getStringTask is complete.  
//  - Meanwhile, control returns to the caller of AccessTheWebAsync.  
//  - Control resumes here when getStringTask is complete.   
//  - The await operator then retrieves the string result from getStringTask.  
string urlContents = await getStringTask;  

// 6. The return statement specifies an integer result.  
// Any methods that are awaiting AccessTheWebAsync retrieve the length value.  
return urlContents.Length;  
} 

1 个答案:

答案 0 :(得分:0)

await会说,等待。当您按下await时,它不会转到调用方方法,它会一直等到Todo完成。现在,如果等待,为什么还要使用async/await?因为在此等待期间,它会执行其他工作。它使您的UI保持运行状态。它处理来自消息队列的消息,例如单击按钮,绘制控件,计算光标是否碰到任何会突出显示控件的边框等。

您的代码现在完全同步,因为您没有等待Task.Delay。您甚至可能会为每个未等待的方法收到警告。

以下是该代码的执行方式:

  1. 等待3000毫秒
  2. 返回“从异步任务返回”
  3. 等待500毫秒
  4. 打印“从异步任务返回”
  5. 打印“终止”

代码:

private async void btn1_Click(object sender, EventArgs e)
{
   await TestAsync();
   Console.WriteLine("terminate");
}

private async Task TestAsync()
{
   string str = await Todo();
   await Task.Delay(500);
   Console.WriteLine(str);
}

private async Task<string> Todo()
{
   await Task.Delay(3000);
   return "return from async task";
}