.net - C# async/await strange behavior in console app -
i built async/await demo console app , strange result. code:
class program { public static void beginlongio(action act) { console.writeline("in beginlongio start... {0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); thread.sleep(1000); act(); console.writeline("in beginlongio end... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); } public static int32 endlongio() { console.writeline("in endlongio start... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); thread.sleep(500); console.writeline("in endlongio end... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); return 42; } public static task<int32> longioasync() { console.writeline("in longioasync start... {0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); var tcs = new taskcompletionsource<int32>(); beginlongio(() => { try { tcs.trysetresult(endlongio()); } catch (exception exc) { tcs.trysetexception(exc); } }); console.writeline("in longioasync end... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); return tcs.task; } public async static task<int32> doasync() { console.writeline("in doasync start... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); var res = await longioasync(); thread.sleep(1000); console.writeline("in doasync end... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); return res; } static void main(string[] args) { ticks = datetime.now.ticks; console.writeline("in main start... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); doasync(); console.writeline("in main exec... \t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); thread.sleep(3000); console.writeline("in main end... \t\t{0} {1}", (datetime.now.ticks - ticks) / timespan.tickspermillisecond, thread.currentthread.managedthreadid); } private static int64 ticks; }
the result bellow:
maybe not understand makes await. thought if execution comes await execution returns caller method , task awaiting runs in thread. in example operations execute in 1 thread , execution doesn't returns caller method after await keyword. truth?
this isn't how async-await
works.
marking method async
doesn't create background threads. when call async
method runs synchronously until asynchronous point , returns caller.
that asynchronous point when await
task haven't completed yet. when complete rest of method scheduled executed. task should represent actual asynchronous operation (like i/o, or task.delay
).
in code there no asynchronous point, there's no point in calling thread returned. thread goes deeper , deeper , blocks on thread.sleep
until these methods completed , doasync
returns.
take simple example:
public static void main() { mainasync().wait(); } public async task mainasync() { // calling thread await task.delay(1000); // different threadpool thread }
here have actual asynchronous point (task.delay
) calling thread returns main
, blocks synchronously on task. after second task.delay
task completed , rest of method executed on different threadpool
thread.
if instead of task.delay
have used thread.sleep
run on same calling thread.
Comments
Post a Comment