Task.WhenAll, Task.WhenAny, and Task.Run in C#
Task.WhenAll runs multiple tasks concurrently and waits for all of them to finish. Task.WhenAny returns as soon as the first task completes. Task.Run schedules synchronous, CPU-bound work on a thread pool thread.
Choosing the right one depends on whether you need all results, just the fastest result, or need to move CPU work off the calling thread.
On this page
- Task.WhenAll: Wait for All Tasks
- Collecting Results with Task.WhenAll
- Exception Handling with Task.WhenAll
- Task.WhenAny: First Completed Task
- Timeout Pattern with Task.WhenAny
- Parallel Async Work with a List
- Task.Run: Offload CPU-Bound Work
- Combining Task.Run with Task.WhenAll
- Comparison: WhenAll vs WhenAny vs Task.Run
- Key Takeaways
Task.WhenAll: Wait for All Tasks
Task.WhenAll starts all tasks at once and suspends until every task has completed. The total time is determined by the slowest task, not the sum of all tasks.
Collecting Results with Task.WhenAll
When all tasks return the same type, Task.WhenAll returns an array of results in the same order the tasks were passed in.
Exception Handling with Task.WhenAll
When one or more tasks fail, await Task.WhenAll(...) re-throws the first exception. To inspect all failures, catch the Task before awaiting and read its Exception.InnerExceptions.
Task.WhenAny: First Completed Task
Task.WhenAny returns a Task<Task> that completes as soon as the first task finishes. The returned value is the completed task, which you then await to get its result or exception.
Timeout Pattern with Task.WhenAny
Combine Task.WhenAny with Task.Delay to implement a timeout without a CancellationToken.
Parallel Async Work with a List
Use Select + Task.WhenAll to fan out over a collection and gather results.
Task.Run: Offload CPU-Bound Work
Task.Run schedules a delegate on the thread pool. Use it when you have synchronous, CPU-intensive code and want to keep the calling thread (UI thread or ASP.NET request thread) free.
Combining Task.Run with Task.WhenAll
Run multiple CPU-bound operations concurrently by combining Task.Run with Task.WhenAll.
Comparison: WhenAll vs WhenAny vs Task.Run
Task.WhenAll | Task.WhenAny | Task.Run | |
|---|---|---|---|
| Completes when | All tasks finish | First task finishes | Scheduled work completes |
| Best for | Fan-out async I/O, collecting results | Race patterns, timeouts, first-available | CPU-bound work off calling thread |
| Returns | Task / Task<T[]> | Task<Task> / Task<Task<T>> | Task / Task<T> |
| Exception behavior | Throws first; all failures in .Exception | Throws if winner faulted | Throws as AggregateException |
| Cancellation | Pass CancellationToken to each task | Check if winner is cancelled task | Pass CancellationToken to Task.Run |
Key Takeaways
- Use
Task.WhenAllwhen you need all results and want them concurrently. - Use
Task.WhenAnyfor race patterns: timeout, first-available replica, progress tracking. - Use
Task.Runto move CPU-bound synchronous code off the calling thread; avoid it for I/O tasks that already have async APIs. - Always
awaitthe task returned byTask.WhenAnya second time to propagate exceptions from the winner. - To capture all exceptions from
Task.WhenAll, store the task before awaiting and inspect.Exception.InnerExceptions.
Take It Further

Struggling with async/await? The definitive recipe book for every concurrency pattern in .NET.
Concurrency in C# Cookbook: 2nd Edition · Stephen Cleary
Get it on Amazon →As an Amazon Associate I earn from qualifying purchases.
Related Articles
Learn how async and await enable asynchronous programming in C#, letting methods suspend execution without blocking the calling thread.
Learn when to use async/await for I/O-bound work versus Task.Run for CPU-bound code that needs to run on a background thread in C#.
Handle exceptions in C# using try-catch-finally. Code in try runs first, catch handles exceptions, and finally always executes for resource cleanup.