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)
- the program starts
main()method. callcount()method called.- the task created, in same thread.
- then task started. @ point, new thread created running
count()method in parallel. - execution continues in callcount(), loop executed , "just before await" printed.
- then
await task;reached. when async-await pattern plays role.awaitnotwait(), doesn't block current thread until task finishes, returns execution controlmain()method , remaining instructions incallcount()(in caseconsole.writeline("callcount completed");) executed after task completed. - in
main(), callcallcount()returnstask(with remaining instructions ofcallcount(), original task) , execution continues. - if dont wait task finishes, execution in
main()continue finalizing program , tasks being destroyed. - if call
wait()(ifcallcount()void dont have task wait for) let task complete, holding inmain()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.

Comments
Post a Comment