我从来没有很好的机会深入了解
所以我在WinForms应用程序中尝试了一下,如下所示:
private async void button2_Click(object sender, EventArgs e)
{
// In below line I understand the Task is created and scheduled to execute, which in this
// simple case means, that it executes right away asynchronously.
var task = Task.Factory.StartNew(() =>
{
Task.Delay(5000).Wait();
return 12;
});
// Here we wait for the task to finish, so we don't see MessageBox yet.
var res = await task;
MessageBox.Show("Result is :" + res);
}
我的问题是,由于我们正在等待 但是,令我惊讶的是,windows的响应能力很强,一切都运行得很好,但我没想到会这样。谁能解释一下这是怎么回事? 读完这篇文章后,我仍然有一个疑问,如果
异步方法很像生成器方法。编译器将在每个
提前返回是异步方法的全部要点。