c# - Callback with async and await -


i have questions regarding execution order of async jobs.

i ask question example because easier understandable.

it official example https://msdn.microsoft.com/en-us/library/mt674882.aspx twist.

async task<int> accessthewebasync() {      httpclient client = new httpclient();      //async operation:     task<string> getstringtask = client.getstringasync("http://msdn.microsoft.com");      // can work here doesn't rely on string getstringasync.     dowork1();      // await operator suspends accessthewebasync.     string urlcontents = await getstringtask;      dowork2();      return urlcontents.length; } 

can dowork2 callback of client.getstringasync?

if so, dowork2 not executed following completion of client.getstringasync if dowork1 runs longer time client.getstringasync.

am right here?

dowork2 not executed following completion of client.getstringasync if dowork1 runs longer time client.getstringasync

once hit point await client.getstrinkasync, dowork1() has completed, example looks it's execution synchronous. once client.getstringasync completes, dowork2 set execute.

the execution flow be:

  1. getstringasync asynchronously starts. executed until hits it's first internal await, , yields control accessthewebasync.
  2. dowork1() kicks off. it's synchronous, waits it's completion
  3. client.getstringasync asynchronously waited completion. await naturally yield control of execution it's caller.
  4. once client.getstringasync completes, execution return accessthewebasync , dowork2() execute synchronously.
  5. urlcontents.length returned.

generally, after first await keyword set continuation rest of method.


Comments