I\'m going to be trying to give a talk on async
-await
and I\'m creating a flow chart that attempts to show the possible orders of execution.
The answer of usr is basically correct, though I think it makes too strong an analogy between threads and tasks. A task need not be anything like another thread. Remember, threads are workers, tasks are jobs. You can have a hundred things on your to-do list without hiring any workers to do them. Try to not think of tasks as lightweight workers, because they are not. They are jobs that need to be done; what worker does them is up to the code that handed you the task.
Your diagram starts off fine but it goes off the rails at "does the caller finish all independent work?" The continuation of the caller is, well, whatever it is. If that continuation involves doing work, it does work. Some of that work might be scheduling tasks to run on the current thread. Some of that work might be keeping the UI responsive.
Also, don't forget that the caller's thread could be terminated and the continuation of the task could be scheduled to another thread.
There are many, many things that can happen here; without understanding what exactly the caller is doing and what the thread context of the caller is, it is impossible to say what happens immediately after the await returns.
This
is vague and seems not correct.
What happens next inside the async method is not dependent on the caller. The method now is an independent agent (like a thread) that runs on its own. It has returned a Task
that is a handle to itself. The caller can do with that task as he pleases (e.g. wait for it, await it, ...).
But if the caller simply drops that Task, the async methods keeps running.
The "re-enter" part of your picture happens at a time controlled by the awaited awaitable. Often, this is some external event such as a completed IO or a timer. The async method now resumes execution not knowing or caring who re-activated it.
Think of each async method as an independent thread. Each await
logically is a Thread.Join()
.