I have questions regarding the execution order of async jobs.
I will ask my question with example because it is easier to be understandable.
It is an official example from https://msdn.microsoft.com/en-us/library/mt674882.aspx with some twist.
async Task<int> AccessTheWebAsync()
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
// The await operator suspends AccessTheWebAsync.
string urlContents = await getStringTask;
DoWork2is not immediately executed following the completion of
DoWork1runs longer time than
Once you hit that point
DoWork1() has already completed, as from your example it looks as it's execution is synchronous. Once
client.GetStringAsync completes, then
DoWork2 is set to execute.
The execution flow will be:
GetStringAsyncasynchronously starts. It is executed until it hits it's first internal
await, and then yields control back to
DoWork1()kicks off. As it's synchronous, everyone waits for it's completion
client.GetStringAsyncis asynchronously waited for completion.
awaitwill naturally yield the control of execution to it's caller.
client.GetStringAsynccompletes, execution will return to
DoWork2()will execute synchronously.
urlContents.lengthwill be returned.
Generally, everything after the first
await keyword is set to be the continuation for the rest of the method.