When an await
is encountered by a running thread, what mechanism is used internally to "bookmark" that that particular method will eventually need to resume when the awaited task is done?
Consider the following method:
public async void DoSomething()
{
await Task.Run(() => SomeLongRunningWork());
// Code to resume when the task is done...
Console.WriteLine("Resuming...");
}
When the above await
is reached, a thread on the thread pool is used to do the long running work.
Furthermore, the calling thread immediately exits the DoSomething() method, and will "come back" when the task is done.
How does the calling thread know how to "come back" at some point? What is happening internally to accomplish this?