Can anyone explain to me how this makes sense? It was an answer in another post that I asked (Does await new Task(async () => Actually halt execution until finished?) and I'm not really understanding part of the comment...
An asynchronous wait -- await -- waits for a task to complete. That's why its called "await".
It is asynchronous because if the task is not complete then await returns to the caller and does other work on this thread until the task is complete, at which time the work that follows the await is scheduled to execute.
Make sure you understand that await is an operator on tasks. It is not a calling convention on calls. Any expression of awaitable type can be awaited; don't fall into the trap of believing that await makes a call asynchronous. The called method is already asynchronous; the task that is returned is the thing that is awaited.
How does it "wait for the task to complete" but at the same time "returns to the caller and does other work"?
Thanks!
FOLLOW-UP:
I've seen the responses, so what should happen in the following pseudo-code example? Which tasks in which order would actually happen?
await new Task(async () =>
{
//Do stuff 1
Thread.Sleep(10000);
}
EventLog.Write("Do Stuff 2");
await new Task(async () =>
{
//Do stuff 3
Thread.Sleep(10000);
}
Would "Do Stuff 1" execute, then wait, then "Do Stuff 2" would happen, then "Do Stuff 3"? Or would "Do Stuff 1" execute and then immediately "Do Stuff 2" followed by "Do Stuff 3"?