async据我了解,主要的await事情之一是让代码易于编写和阅读——但使用它们是否等于产生后台线程来执行长时间的逻辑?
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编译器会在后台生成状态机。
这是一个例子,我希望我可以解释一些正在发生的高级细节:
public async Task MyMethodAsync() { Task<int> longRunningTask = LongRunningOperationAsync(); // independent work which doesn't need the result of LongRunningOperationAsync can be done here //and now we call await on the task int result = await longRunningTask; //use the result Console.WriteLine(result); } public async Task<int> LongRunningOperationAsync() // assume we return an int from this long running operation { await Task.Delay(1000); // 1 second delay return 1; }
好的,那么这里会发生什么:
Task<int> longRunningTask = LongRunningOperationAsync();开始执行LongRunningOperation
Task<int> longRunningTask = LongRunningOperationAsync();
LongRunningOperation
独立工作完成让我们假设主线程(线程 ID = 1)然后await longRunningTask到达。
await longRunningTask
现在,如果longRunningTask尚未完成并且仍在运行,MyMethodAsync()将返回其调用方法,因此主线程不会被阻塞。完成longRunningTask后,来自 ThreadPool 的线程(可以是任何线程)将返回MyMethodAsync()其先前的上下文并继续执行(在这种情况下将结果打印到控制台)。
longRunningTask
MyMethodAsync()
第二种情况是longRunningTask已经完成执行并且结果可用。到达时await longRunningTask我们已经有了结果,因此代码将继续在同一个线程上执行。(在这种情况下,将结果打印到控制台)。当然,上面的例子不是这种情况,其中Task.Delay(1000)涉及到。
Task.Delay(1000)