c# - Strange behavior of await -


i have async method this

public async task<bool> fooasync()  {      return await somethirdpartylib.somemethodreturningboolasync().configureawait(false); } 

i have code invokes in loop:

for (var = 0; < 10; i++) {     ok &= await fooasync().configureawait(false); } 

in case hang process after 2 or 3 or amount of cycles. when change code to

for (var = 0; < 10; i++) {     ok &= await fooasync()         .continuewith(t => {             if (t.iscompleted)             {                 return t.result;             }             return false;         })         .configureawait(false); } 

the exact same code works. ideas?

edit: changed sample code little bit show fooasync in principle. in reply given answers , comments: don't know somemethodreturningboolasync does. fact continuewith in fact nothing useful strucked me.

your fooasync() despite name, doing work synchronously, since you're starting work on ui thread, continue of work on ui thread.

when add in continuewith you're forcing method (that nothing productive) run on thread pool thread, first fooasync call running on ui thread. subsequent calls on thread pool thread result of configureawait(false).

the correct fix adjust fooasync work asynchronously, rather synchronously, or if doesn't conceptually asynchronous work, should synchronous method, not return task, , called task.run in method here, since needs synchronous work in thread.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -