Task.WhenAll Method (Task[])

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a task that will complete when all of the Task objects in an array have completed.

Namespace:   System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

public static Task WhenAll(
	params Task[] tasks


Type: System.Threading.Tasks.Task[]

The tasks to wait on for completion.

Return Value

Type: System.Threading.Tasks.Task

A task that represents the completion of all of the supplied tasks.

Exception Condition

The tasks argument was null.


The tasks array contained a null task.

The overloads of the WhenAll method that return a Task object are typically called when you are interested in the status of a set of tasks or in the exceptions thrown by a set of tasks.


The call to WhenAll(Task[]) method does not block the calling thread.

If any of the supplied tasks completes in a faulted state, the returned task will also complete in a TaskStatus.Faulted state, where its exceptions will contain the aggregation of the set of unwrapped exceptions from each of the supplied tasks.

If none of the supplied tasks faulted but at least one of them was canceled, the returned task will end in the TaskStatus.Canceled state.

If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the TaskStatus.RanToCompletion state.

If the supplied array/enumerable contains no tasks, the returned task will immediately transition to a TaskStatus.RanToCompletion state before it's returned to the caller.

The following example creates a set of tasks that ping the URLs in an array. The tasks are stored in a List<Task> collection that is converted to an array and passed to the WhenAll(IEnumerable<Task>) method. After the call to the Wait method ensures that all threads have completed, the example examines the Task.Status property to determine whether any tasks have faulted.

using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;

public class Example
   public static void Main()
      int failed = 0;
      var tasks = new List<Task>();
      String[] urls = { "www.adatum.com", "www.cohovineyard.com",
                        "www.cohowinery.com", "www.northwindtraders.com",
                        "www.contoso.com" };

      foreach (var value in urls) {
         var url = value;
         tasks.Add(Task.Run( () => { var png = new Ping();
                                     try {
                                        var reply = png.Send(url);
                                        if (! (reply.Status == IPStatus.Success)) {
                                           Interlocked.Increment(ref failed);
                                           throw new TimeoutException("Unable to reach " + url + ".");
                                     catch (PingException) {
                                        Interlocked.Increment(ref failed);
      Task t = Task.WhenAll(tasks.ToArray());
      try {
      catch {}   

      if (t.Status == TaskStatus.RanToCompletion)
         Console.WriteLine("All ping attempts succeeded.");
      else if (t.Status == TaskStatus.Faulted)
         Console.WriteLine("{0} ping attempts failed", failed);      
// The example displays output like the following:
//       5 ping attempts failed

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top