根据我的理解,async和await所做的主要事情之一是使代码易于编写和阅读-但使用它们是否等于生成后台线程来执行长时间的逻辑?
我目前正在尝试最基本的例子。我内联添加了一些注释。你能给我解释一下吗?
// I don't understand why this method must be marked as `async`.
private async void button1_Click(object sender, EventArgs e)
{
Task<int> access = DoSomethingAsync();
// task independent stuff here
// this line is reached after the 5 seconds sleep from
// DoSomethingAsync() method. Shouldn't it be reached immediately?
int a = 1;
// from my understanding the waiting should be done here.
int x = await access;
}
async Task<int> DoSomethingAsync()
{
// is this executed on a background thread?
System.Threading.Thread.Sleep(5000);
return 1;
}
根据我的理解,async和await所做的主要事情之一是使代码易于编写和阅读。
它们是为了让异步代码易于编写和阅读。
这和生成后台线程来执行长时间逻辑是一样的吗?
一点也不。
//我不明白为什么这个方法必须被标记为'async'。
async关键字启用await关键字。所以任何使用await的方法都必须被标记为async。
// DoSomethingAsync()方法在5秒休眠后到达该行。难道不应该立即到达吗?
不会,因为异步方法默认情况下不会在另一个线程上运行。
//是否在后台线程上执行?
No.
你可能会发现我的async/await介绍很有用。官方MSDN文档也非常好(尤其是TAP部分),异步团队还发布了一个很好的FAQ。
除了其他答案,还有await (c#参考)
更具体地说,在包含的例子中,它解释了您的情况
下面的Windows窗体示例说明了await在
异步方法,WaitAsynchronouslyAsync。对比一下它的行为
方法使用waitsynchrontically的行为。没有等待
应用到任务的操作符,waitsynchronize同步运行
尽管在定义中使用了async修饰符,并且调用了
线程。睡在它的身体里。
private async void button1_Click(object sender, EventArgs e)
{
// Call the method that runs asynchronously.
string result = await WaitAsynchronouslyAsync();
// Call the method that runs synchronously.
//string result = await WaitSynchronously ();
// Display the result.
textBox1.Text += result;
}
// The following method runs asynchronously. The UI thread is not
// blocked during the delay. You can move or resize the Form1 window
// while Task.Delay is running.
public async Task<string> WaitAsynchronouslyAsync()
{
await Task.Delay(10000);
return "Finished";
}
// The following method runs synchronously, despite the use of async.
// You cannot move or resize the Form1 window while Thread.Sleep
// is running because the UI thread is blocked.
public async Task<string> WaitSynchronously()
{
// Add a using directive for System.Threading.
Thread.Sleep(10000);
return "Finished";
}