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?
dowork2not executed following completion ofclient.getstringasyncifdowork1runs longer timeclient.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:
getstringasyncasynchronously starts. executed until hits it's first internalawait, , yields controlaccessthewebasync.dowork1()kicks off. it's synchronous, waits it's completionclient.getstringasyncasynchronously waited completion.awaitnaturally yield control of execution it's caller.- once
client.getstringasynccompletes, execution returnaccessthewebasync,dowork2()execute synchronously. urlcontents.lengthreturned.
generally, after first await keyword set continuation rest of method.
Comments
Post a Comment