Task.WaitAll Method

Definition

Waits for all of the provided Task objects to complete execution.

Overloads

WaitAll(Task[], Int32, CancellationToken)

Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled.

WaitAll(ReadOnlySpan<Task>)

Waits for all of the provided Task objects to complete execution.

WaitAll(Task[])

Waits for all of the provided Task objects to complete execution.

WaitAll(IEnumerable<Task>, CancellationToken)

Waits for all of the provided Task objects to complete execution unless the wait is cancelled.

WaitAll(Task[], Int32)

Waits for all of the provided Task objects to complete execution within a specified number of milliseconds.

WaitAll(Task[], CancellationToken)

Waits for all of the provided Task objects to complete execution unless the wait is cancelled.

WaitAll(Task[], TimeSpan)

Waits for all of the provided cancellable Task objects to complete execution within a specified time interval.

WaitAll(Task[], Int32, CancellationToken)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled.

C#
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);

Parameters

tasks
Task[]

An array of Task instances on which to wait.

millisecondsTimeout
Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the tasks to complete.

Returns

true if all of the Task instances completed execution within the allotted time; otherwise, false.

Attributes

Exceptions

One or more of the Task objects in tasks has been disposed.

The tasks argument is null.

At least one of the Task instances was canceled. If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.

-or-

An exception was thrown during the execution of at least one of the Task instances.

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

The tasks argument contains a null element.

The cancellationToken was canceled.

Remarks

The cancellationToken argument is used to cancel the wait operation. Cancellation of the tasks is a distinct operation, and is signaled by the AggregateException noted above.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WaitAll(ReadOnlySpan<Task>)

Waits for all of the provided Task objects to complete execution.

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll(scoped ReadOnlySpan<System.Threading.Tasks.Task> tasks);

Parameters

tasks
ReadOnlySpan<Task>

An array of Task instances on which to wait.

Attributes

Exceptions

The tasks argument contains a null element.

At least one of the Task instances was canceled.

-or-

An exception was thrown during the execution of at least one of the Task instances.

Applies to

.NET 9
Product Versions
.NET 9

WaitAll(Task[])

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Waits for all of the provided Task objects to complete execution.

C#
public static void WaitAll(params System.Threading.Tasks.Task[] tasks);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll(params System.Threading.Tasks.Task[] tasks);

Parameters

tasks
Task[]

An array of Task instances on which to wait.

Attributes

Exceptions

One or more of the Task objects in tasks has been disposed.

The tasks argument is null.

The tasks argument contains a null element.

At least one of the Task instances was canceled. If a task was canceled, the AggregateException exception contains an OperationCanceledException exception in its InnerExceptions collection.

-or-

An exception was thrown during the execution of at least one of the Task instances.

Examples

The following example starts 10 tasks, each of which is passed an index as a state object. Tasks with an index from two to five throw exceptions. The call to the WaitAll method wraps all exceptions in an AggregateException object and propagates it to the calling thread.

C#
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

class Example
{
    static void Main()
    {
        var tasks = new List<Task<int>>();
         
        // Define a delegate that prints and returns the system tick count
        Func<object, int> action = (object obj) =>
        {
            int i = (int)obj;

            // Make each thread sleep a different time in order to return a different tick count
            Thread.Sleep(i * 100);

            // The tasks that receive an argument between 2 and 5 throw exceptions
            if (2 <= i && i <= 5)
            {
                throw new InvalidOperationException("SIMULATED EXCEPTION");
            }

            int tickCount = Environment.TickCount;
            Console.WriteLine("Task={0}, i={1}, TickCount={2}, Thread={3}", Task.CurrentId, i, tickCount, Thread.CurrentThread.ManagedThreadId);

            return tickCount;
        };

        // Construct started tasks
        for (int i = 0; i < 10; i++)
        {
            int index = i;
            tasks.Add(Task<int>.Factory.StartNew(action, index));
        }

        try
        {
            // Wait for all the tasks to finish.
            Task.WaitAll(tasks.ToArray());

            // We should never get to this point
            Console.WriteLine("WaitAll() has not thrown exceptions. THIS WAS NOT EXPECTED.");
        }
        catch (AggregateException e)
        {
            Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)");
            for (int j = 0; j < e.InnerExceptions.Count; j++)
            {
                Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
            }
        }
    }
}
// The example displays output like the following:
//     Task=1, i=0, TickCount=1203822250, Thread=3
//     Task=2, i=1, TickCount=1203822359, Thread=4
//     Task=7, i=6, TickCount=1203823484, Thread=3
//     Task=8, i=7, TickCount=1203823890, Thread=4
//     Task=9, i=8, TickCount=1203824296, Thread=3
//     Task=10, i=9, TickCount=1203824796, Thread=4
//     
//     The following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()
//     
//     -------------------------------------------------
//     System.InvalidOperationException: SIMULATED EXCEPTION
//        at Example.<Main>b__0(Object obj)
//        at System.Threading.Tasks.Task`1.InnerInvoke()
//        at System.Threading.Tasks.Task.Execute()

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WaitAll(IEnumerable<Task>, CancellationToken)

Waits for all of the provided Task objects to complete execution unless the wait is cancelled.

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll(System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> tasks, System.Threading.CancellationToken cancellationToken = default);

Parameters

tasks
IEnumerable<Task>

A collection of tasks on which to wait.

cancellationToken
CancellationToken

A token to observe while waiting for the tasks to complete.

Attributes

Exceptions

The tasks argument is null.

The tasks argument contains a null element.

One or more of the Task objects in tasks has been disposed.

The cancellationToken was canceled.

At least one of the Task instances was canceled. If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.

Applies to

.NET 9
Product Versions
.NET 9

WaitAll(Task[], Int32)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Waits for all of the provided Task objects to complete execution within a specified number of milliseconds.

C#
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);

Parameters

tasks
Task[]

An array of Task instances on which to wait.

millisecondsTimeout
Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

Returns

true if all of the Task instances completed execution within the allotted time; otherwise, false.

Attributes

Exceptions

One or more of the Task objects in tasks has been disposed.

The tasks argument is null.

At least one of the Task instances was canceled. If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.

-or-

An exception was thrown during the execution of at least one of the Task instances.

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

The tasks argument contains a null element.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WaitAll(Task[], CancellationToken)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Waits for all of the provided Task objects to complete execution unless the wait is cancelled.

C#
public static void WaitAll(System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void WaitAll(System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);

Parameters

tasks
Task[]

An array of Task instances on which to wait.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the tasks to complete.

Attributes

Exceptions

The cancellationToken was canceled.

The tasks argument is null.

At least one of the Task instances was canceled. If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.

-or-

An exception was thrown during the execution of at least one of the Task instances.

The tasks argument contains a null element.

One or more of the Task objects in tasks has been disposed.

Remarks

The cancellationToken argument is used to cancel the wait operation. Cancellation of the tasks is a distinct operation, and is signaled by the AggregateException as noted above.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WaitAll(Task[], TimeSpan)

Source:
Task.cs
Source:
Task.cs
Source:
Task.cs

Waits for all of the provided cancellable Task objects to complete execution within a specified time interval.

C#
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool WaitAll(System.Threading.Tasks.Task[] tasks, TimeSpan timeout);

Parameters

tasks
Task[]

An array of Task instances on which to wait.

timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

Returns

true if all of the Task instances completed execution within the allotted time; otherwise, false.

Attributes

Exceptions

One or more of the Task objects in tasks has been disposed.

The tasks argument is null.

At least one of the Task instances was canceled. If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.

-or-

An exception was thrown during the execution of at least one of the Task instances.

timeout is a negative number other than -1 milliseconds, which represents an infinite time-out.

-or-

timeout is greater than Int32.MaxValue.

The tasks argument contains a null element.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0