I am trying to understand how Async and Await works. How control travel in the program. Here is the code which I was trying to understand.
public async Task
Behind the scenes C# compiler actually converts your code into a state machine. It generates a lot more code so that behind the scenes every time a await task or async action is completed, it'll continue execution from where it left off. In terms of your question, every time the async action has finished, the async method will be called back on the calling thread when you originally started the call to the async method. Eg it'll execute your code on the thread that you started on. So the async action will be run on a Task
thread, then the result will be returned back on the thread you method was originally called on and keep executing.
Await
will get the value from the Task
or async action and "unbox" it from the task when the execution is returned. In this case it will automatically put it into the int value, so no need to store the Task<int>
.
Your code has the problem where it await's on the LongRunningTask()
you'd most likely just want to return the long task method without the async
, then have your MyMethod
perform the await.
int value = await LongWaitingTask()
Async Await and the Generated StateMachine
It's a requirement of async
methods that you return a Task
or void
.
It's possible to change it so when you return back from executing the async task it will execute the remaining code on the thread the async task was performed on using the Task.ConfigureAwait method.
I have an async intro on my blog that you may find helpful.
This code:
int result = await LongRunningOperation();
is essentially the same as this code:
Task<int> resultTask = LongRunningOperation();
int result = await resultTask;
So, yes, LongRunningOperation
is invoked directly by that method.
When the await
operator is passed an already-completed task, it will extract the result and continue executing the method (synchronously).
When the await
operator is passed an incomplete task (e.g., the task returned by LongRunningOperation
will not be complete), then by default await
will capture the current context and return an incomplete task from the method.
Later, when the await
task completes, the remainder of the method is scheduled to run in that context.
This "context" is SynchronizationContext.Current
unless it is null
, in which case it is TaskScheduler.Current
. If you're running this in a Console app, then the context is usually the thread pool context, so the async
method will resume executing on a thread pool thread. However, if you execute the same method on a UI thread, then the context is a UI context and the async
method will resume executing on the UI thread.
I've been taught about it in the following fashion, I found it to be quite a clear and concise explanation:
//this is pseudocode
async Method()
{
code;
code;
await something;
moreCode;
}
When Method
is invoked, it executes its contents (code;
lines) up to await something;
. At that point, something;
is fired and the method ends like a return;
was there.
something;
does what it needs to and then returns.
When something;
returns, execution gets back to Method
and proceeds from the await
onward, executing moreCode;
In a even more schematic fashion, here's what happens:
code;
is executedsomething;
is executed, flow goes back to the point where Method
was invokedMethod
invocationsomething;
returns, flow returns inside Method
moreCode;
is executed and Method
itself ends (yes, there could be something else await
-ing on it too, and so on and so forth)It may be easier to think about it this way:
Whenever you have an await
, the compiler splits your method into 2: one part before the await
and another part after it.
The second part runs after the first one has finished successfully.
In your code, the first method will look like something roughly equivalent to this:
public async Task MyMethod()
{
Task<int> longRunningTask = LongRunningOperation();
MySynchronousMethod();
longRunningTask.ContinueWith(t => part2(t.Result));
}
void part2(int result)
{
Console.WriteLine(result);
}
Few important notes:
Task
is not actually being used directly. It's using the task's GetAwaiter()
method and its API, or any other class with this method or extension method.MyMethod
is being split, how does someone who awaits MyMethod
knows when all parts are done? When your async method returns a Task
, the compiler generates a special Task
which tracks everything with a state machine.