c# - Task.Delay not returning to main thread -


I'm checking the async / wait and here's the problem. I am starting two tasks asynchronous and are waiting to complete their execution. I hope wait for two work-related delays and then print out the total milliseconds. But the program only starts with 1 + "current time and 2 starts" + current time but after that nothing. How can I return to delays and how can I reach the end result?

  Fixed zero main (string [] arg) {RunTasksWithDelays (); } Static async Zero Run Task with Dellies () {Stopwatch S = Stopwatch. Startup (); Console.WriteLine (Thread.CurrentThread.ManagedThreadId); Task Task 1 = LonglingTask 1 (); Task Task 2 = LonglingTask2 (); Awaiting work When All (Job 1, Work 2); Console.print line ("total milliseconds elapsed:" + s.Elapsed millisecond / 1000); } Fixed async work LongRunningTask1 () {Console.WriteLine ("1 start" + date time.NEW); Awaiting work Daley (5000); Console.WriteLine (Thread.CurrentThread.ManagedThreadId); Console.light line ("1 end" + date time.NEW); } Fixed async Task LangeringTest2 () {console. Weedline ("2 start" + date time.NEW); Awaiting work Daley (2000); Console.WriteLine (Thread.CurrentThread.ManagedThreadId); Console.light line ("2 end" + date time.NEW); }  

Your main thread ends and thus the program ends in your No foreground thread is the application to keep the program alive.

Ideally you should wait for the RunTasksWithDelays method, but you can not wait for it two reasons

  1. Your method < Code> Work
  2. The Main method does not support returning to .

You should do async zero instead of the RuntensCard with release method return async task and you should wait for its end. Waiting for synchronization is a bad idea, right in this scenario because we do not have any choice.

  Fixed zero main (string [] args) {RunTasksWithDelays (). } Static async Task Run Task with Dellies () {...}  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -