asynchronous - C# Async and Wait Not Working -


why following async , await not working? trying learn understand wrong code.

class program {     static void main(string[] args)     {          callcount();      }      static void count()     {         (int = 0; < 5; i++)         {             system.threading.thread.sleep(2000);             console.writeline("count loop: " + i);         }     }      static async void callcount()     {         task task = new task(count);         task.start();         (int = 0; < 3; i++)         {             system.threading.thread.sleep(4000);             console.writeline("writing callcount loop: " + i);         }         console.writeline("just before await");         await task;         console.writeline("callcount completed");     } } 

the program goes starts count() method drops out without completing it. await task; statement expecting wait complete loops of count() method (0, 1, 2, 3, 4) before exiting. "count loop: 0". going through of callcount(). await task isn't doing anything. want both count() , callcount() run asynchronously , return main when complete.

when execute async method, starts running synchronously until reaches await statement, rest of code executes asynchronously, , execution return caller.

in code callcount() starts running synchronously await task, main() method, , since not waiting method complete, program ends without method count() can finish.

you can see desired behavior changing return type task, , calling wait() in main() method.

static void main(string[] args) {     callcount().wait(); }  static void count() {     (int = 0; < 5; i++)     {         system.threading.thread.sleep(2000);         console.writeline("count loop: " + i);     } }  static async task callcount() {     task task = new task(count);     task.start();     (int = 0; < 3; i++)     {         system.threading.thread.sleep(1000);         console.writeline("writing callcount loop: " + i);     }     console.writeline("just before await");     await task;     console.writeline("callcount completed"); } 

edit: how code executes:

(for better understanding lets changes callcount() return type task)

  1. the program starts main() method.
  2. callcount() method called.
  3. the task created, in same thread.
  4. then task started. @ point, new thread created running count() method in parallel.
  5. execution continues in callcount(), loop executed , "just before await" printed.
  6. then await task; reached. when async-await pattern plays role. await not wait(), doesn't block current thread until task finishes, returns execution control main() method , remaining instructions in callcount() (in case console.writeline("callcount completed");) executed after task completed.
  7. in main(), call callcount() returns task (with remaining instructions of callcount() , original task) , execution continues.
  8. if dont wait task finishes, execution inmain() continue finalizing program , tasks being destroyed.
  9. if call wait() (if callcount() void dont have task wait for) let task complete, holding in main() count() execution , "callcount completed" being printed.

if want wait count task finishes in callcount() without returning main() method, call task.wait();, program wait count(), not await do.

this link explains async-await pattern in details.

hopes workflow diagram of code helps you.

enter image description here


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -