Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Async and Concurrency

Task.WhenAll() and Task.WhenAny()

Reference for Task.WhenAll, Task.WhenAny and Task.WhenEach in C#: wait for several tasks, collect results, handle failures and add timeouts.

Task.WhenAll returns a task that completes when every task you give it has completed. Task.WhenAny returns a task that completes as soon as any one of them completes, and its result is that first finished task. Neither method starts any work: the tasks must already be running.

.NET 9 added Task.WhenEach, which hands you the tasks in the order they finish as an IAsyncEnumerable<Task<T>>.

C#
string[] pages = await Task.WhenAll(urls.Select(Download));
Task first = await Task.WhenAny(a, b);

Try it

C# Wait for all, take the first

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Failures and timeouts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static Task          WhenAll(params Task[] tasks);
public static Task<TResult[]> WhenAll<TResult>(params Task<TResult>[] tasks);
public static Task          WhenAll(IEnumerable<Task> tasks);
public static Task<Task>    WhenAny(params Task[] tasks);
public static Task<Task<TResult>> WhenAny<TResult>(params Task<TResult>[] tasks);
public static IAsyncEnumerable<Task<TResult>> WhenEach<TResult>(IEnumerable<Task<TResult>> tasks);
Name Description
tasks The tasks (array, params list or sequence) to combine.

Returns: WhenAll<T> yields an array of results in input order. WhenAny yields the first completed task itself, so you can inspect it or await it for its result.

Errors: ArgumentNullException if the collection or any element is null, and ArgumentException for an empty array passed to WhenAny. If one or more tasks fault, WhenAll faults after all tasks have finished, and await rethrows only the first exception. If none faulted but one was canceled, the result is canceled.

Usage notes

  • Start first, await later. Starting the tasks and then calling WhenAll runs them concurrently. foreach (var x in xs) await Work(x); runs them one after another.
  • WhenAll waits for all tasks, even if one fails early. To stop the others, share a CancellationToken and cancel it when you see the first failure.
  • Inspect every task to see every failure; the Exception property of the combined task has them all.
  • WhenAny does not cancel the losers. They keep running; cancel them with a token or observe them later so their exceptions are not lost.
  • Empty input: WhenAll on an empty sequence returns an already completed task (with an empty array for Task<T>[]).
  • Throttling. Starting thousands of tasks at once can overload a server. Use SemaphoreSlim, Parallel.ForEachAsync or batching.
  • Timeouts. task.WaitAsync(TimeSpan) (.NET 6+) is cleaner than racing against Task.Delay, but it does not stop the underlying work.
  • Task.WhenEach (.NET 9) avoids the old while (list.Count > 0) { var t = await WhenAny(list); list.Remove(t); } loop, which is quadratic.