This section describes the use of two methods, one is waiting for the group to perform all tasks are completed Task.WhenAll()
method, as long as the other is a group perform the method has finished executing Task.WhenAny()
the method.
Specific use, as shown demo code.
static void Main(string[] args) { // first way to run through all the tasks waiting for completion Task.WhenAll var firstTask = new new the Task < int > (() => TaskMethod ( " First the Task " , . 3 )); var SecondTask = new new the Task < int > (() => TaskMethod ( " Second the Task " , 2 )); // When whenAllTask performed only after the operation is completed SecondTask firstTask and the ContinueWith var whenAllTask = Task.WhenAll (firstTask, SecondTask); whenAllTask.ContinueWith (T => the WriteLine ($ " first task answer {t.Result [0]}, the answer to the second task t.Result {[. 1]} " ), TaskContinuationOptions.OnlyOnRanToCompletion); firstTask.Start(); secondTask.Start(); Sleep(TimeSpan.FromSeconds(4)); // Use WhenAny method as long as a task is completed list then the method will remove the task var Tasks = new new List <the Task < int >> (); for ( int I = 0 ; I < . 4 ; I ++ ) { int counter = 1; var task = new Task<int>(() => TaskMethod($"Task {counter}",counter)); tasks.Add(task); task.Start(); } while (tasks.Count > 0) { var completedTask = Task.WhenAny(tasks).Result; tasks.Remove(completedTask); The WriteLine ($ " a task has been completed, the result is completedTask.Result} { " ); } ReadLine(); } static int TaskMethod(string name, int seconds) { The WriteLine ($ " task to run on {CurrentThread.ManagedThreadId} whether the thread pool thread:. CurrentThread.IsThreadPoolThread {} " ); Sleep(TimeSpan.FromSeconds(seconds)); return 42 * seconds; }