This time to introduce some usage:
Await a batch of tasks, do not wait for other tasks to complete, direct follow-up tasks.
classProgram {Static voidMain (string[] args) { varTaska = Delayandrerutnasync (2); varTASKB = Delayandrerutnasync (3); varTASKC = Delayandrerutnasync (1); vartasks =New[] {Taska, TASKB, TASKC}; varProcessingtasks= ( fromTinchTasksSelectAwaitandprocessasync (t)). ToArray (); Task.whenall (Processingtasks); Console.read (); } Static Asynctask<int> Delayandrerutnasync (intval) { awaitTask.delay (Timespan.fromseconds (Val)); Console.WriteLine ("waiting for {0}", Val); returnVal; } Static AsyncTask Awaitandprocessasync (task<int>Task) { varresult =awaittask; Console.WriteLine (result); } }
As you can see from the printed results, after the execution of the method waiting for 1 seconds, there is no waiting for the execution of other methods, but the following async methods are executed directly.
Here's a way to use anonymous methods
classProgram {Static voidMain (string[] args) { varTaska = Delayandrerutnasync (2); varTASKB = Delayandrerutnasync (3); varTASKC = Delayandrerutnasync (1); vartasks =New[] {Taska, TASKB, TASKC}; varProcessingtasks = tasks. Select (AsyncTask = { varresult =awaittask; Console.WriteLine (result); }); Task.whenall (Processingtasks); Console.read (); } Static Asynctask<int> Delayandrerutnasync (intval) { awaitTask.delay (Timespan.fromseconds (Val)); Console.WriteLine ("waiting for {0}", Val); returnVal; } }
C # Asynchronous Programming (II): Asynchronous Base replenishment