i've read eric lippert's article async
, , confusions people had async
keyword. said :
it (
async
) means “this method contains control flow involves awaiting asynchronous operations , therefore rewritten compiler continuation passing style ensure asynchronous operations can resume method @ right spot.” the whole point of async methods stay on current thread as possible
i don't understand this. if execute asynchronous method (task
) , runs , surely runs on thread.
moreover , if write method uses await
, (imho) releases normal control flow , , code refactored alike "continuewith
" later , on another thread.
i tested (console) :
/*1*/ public void startchain() /*2*/ { /*3*/ var = funca(); /*4*/ console.writeline(a.result); /*5*/ } /*6*/ /*7*/ public async task < int > funca() /*8*/ { /*9*/ console.writeline("a--" + thread.currentthread.managedthreadid); /*10*/ var t = await funcb(); /*11*/ console.writeline("b--" + thread.currentthread.managedthreadid); /*12*/ return t; /*13*/ } /*14*/ /*15*/ public async task < int > funcb() /*16*/ { /*17*/ console.writeline("c--" + thread.currentthread.managedthreadid); /*18*/ await task.delay(2000); /*19*/ console.writeline("d--" + thread.currentthread.managedthreadid); /*20*/ return 999; /*21*/ } /*22*/ /*23*/ void main() /*24*/ { /*25*/ startchain(); /*26*/ } /*27*/
the result :
a--7 c--7 d--17 <-----d , b on different thread b--17 999
so did eric mean saying "stay on current thread"?
edit 1:
in asp.net
return differnt thread id.
public async task<int> funca() { response.write("<br/>c----" + thread.currentthread.managedthreadid); var t = await funcb(); response.write("<br/>d----" + thread.currentthread.managedthreadid); return t; } public async task<int> funcb() { response.write("<br/>e----" + thread.currentthread.managedthreadid); await task.delay(2000); response.write("<br/>f----" + thread.currentthread.managedthreadid); return 999; } protected async void page_load(object sender, eventargs e) { response.write("<br/>a----" + thread.currentthread.managedthreadid); var a=await funca(); response.write("<br/>b----" + thread.currentthread.managedthreadid); } a----8 c----8 e----8 f----9 d----9 b----9
edit 2
(after getting answer)
it seems thread served @ gui apps :. run code @ winform
public async task<int> funca() { textbox1.text +=environment.newline+ "\nc----" + thread.currentthread.managedthreadid; var t = await funcb(); textbox1.text += environment.newline + "\nd----" + thread.currentthread.managedthreadid; return t; } public async task<int> funcb() { textbox1.text += environment.newline + "\ne----" + thread.currentthread.managedthreadid; await task.delay(2000); textbox1.text += environment.newline + "\nf----" + thread.currentthread.managedthreadid; return 999; } private async void form1_load(object sender, eventargs e) { textbox1.text += environment.newline + "\na----" + thread.currentthread.managedthreadid; var = await funca(); textbox1.text += environment.newline + "\nb----" + thread.currentthread.managedthreadid; }
the async/await support added programmers write guis don't freeze. particularly useful in store apps, , core reason added c# v5, winrt pretty unfriendly api has many asynchronous methods.
the "stay on same thread" scenario very important in gui app, required because gui isn't thread-safe. require message loop, way asynchronous code resume on same thread. message loop core solution producer-consumer problem. program doesn't have one, looks lot console mode app. therefore don't behavior, resumes on worker thread.
not of problem, don't need resume on same thread since console thread-safe anyway. well, mostly, not counting lock added in .net 4.5 when ask input. of course means don't have heckofalot of use async/await either, task works fine well.
Comments
Post a Comment