derek derek - 2 months ago 21
C# Question

Callback with async and await

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();

//async operation:
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

// You can do work here that doesn't rely on the string from GetStringAsync.
DoWork1();

// The await operator suspends AccessTheWebAsync.
string urlContents = await getStringTask;

DoWork2();

return urlContents.Length;
}


Can I say
DoWork2
is a callback of
client.GetStringAsync
?

If so,
DoWork2
is not immediately executed following the completion of
client.GetStringAsync
IF
DoWork1
runs longer time than
client.GetStringAsync
.

Am I right here?

Answer

DoWork2 is not immediately executed following the completion of client.GetStringAsync if DoWork1 runs longer time than client.GetStringAsync

Once you hit that point await client.GetStrinkAsync, 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:

  1. GetStringAsync asynchronously starts. It is executed until it hits it's first internal await, and then yields control back to AccessTheWebAsync.
  2. DoWork1() kicks off. As it's synchronous, everyone waits for it's completion
  3. client.GetStringAsync is asynchronously waited for completion. await will naturally yield the control of execution to it's caller.
  4. Once client.GetStringAsync completes, execution will return to AccessTheWebAsync and DoWork2() will execute synchronously.
  5. urlContents.length will be returned.

Generally, everything after the first await keyword is set to be the continuation for the rest of the method.

Comments