c# - ConfigureAwait(true) not returning on the context it was awaited on -
sample :
static void main(string[] args) { int counter = 0; var t = new system.timers.timer(); t.interval = 3000; t.elapsed += async (sender, e) => { debug.writeline("before await - thread : {0} , counter : {1}", thread.currentthread.managedthreadid, counter); await task.delay(1000).configureawait(true); debug.writeline("ater await thread : {0}, counter : {1} ", thread.currentthread.managedthreadid, counter); counter++; if (counter == 2) { debug.writeline("stop"); t.stop(); } }; t.start(); console.readkey(); }
this outputs :
before await - thread : 4 , counter : 0 ater await thread : 4, counter : 0 before await - thread : 5 , counter : 1 ater await thread : 4, counter : 1 stop
why didn't return on thread await called on. first time timer runs on thread 5 , suppose capture context when async operation ends, , can see returns on thread 4.
there isn't synchronization context in console app.
if run code in winforms , you'd see work expected..
Comments
Post a Comment