When using async
and await
the compiler generates a state machine in the background.
(使用async
并await
,编译器会在后台生成状态机。)
Here's an example on which I hope I can explain some of the high-level details that are going on:
(这是一个例子,我希望我可以解释正在发生的一些高级细节:)
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;
}
OK, so what happens here:
(好,那么这里发生了什么:)
Task<int> longRunningTask = LongRunningOperationAsync();
starts executing LongRunningOperation
(开始执行LongRunningOperation
)
Independent work is done on let's assume the Main Thread (Thread ID = 1) then await longRunningTask
is reached.
(假设主线程(线程ID = 1)完成独立工作,然后await longRunningTask
到达。)
Now, if the longRunningTask
hasn't finished and it is still running, MyMethodAsync()
will return to its calling method, thus the main thread doesn't get blocked.
(现在,如果longRunningTask
尚未完成并且仍在运行,则MyMethodAsync()
将返回其调用方法,因此主线程不会被阻塞。)
When the longRunningTask
is done then a thread from the ThreadPool (can be any thread) will return to MyMethodAsync()
in its previous context and continue execution (in this case printing the result to the console). (当longRunningTask
完成时,来自ThreadPool的线程(可以是任何线程)将在其先前的上下文中返回MyMethodAsync()
并继续执行(在这种情况下,将结果打印到控制台)。)
A second case would be that the longRunningTask
has already finished its execution and the result is available.
(第二种情况是longRunningTask
已经完成其执行并且结果可用。)
When reaching the await longRunningTask
we already have the result so the code will continue executing on the very same thread. (当到达await longRunningTask
我们已经有了结果,因此代码将继续在同一线程上执行。)
(in this case printing result to console). ((在这种情况下,将结果打印到控制台)。)
Of course this is not the case for the above example, where there's a Task.Delay(1000)
involved. (当然,对于上面的示例,情况并非如此,其中涉及Task.Delay(1000)
。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…