Skip to content

Waiting for Multiple Tasks

Question

You have started multiple tasks without awaiting them. How can you wait for all of them?

Short interview answer

Store the tasks and await Task.WhenAll(tasks). It completes after every supplied task completes and propagates failures when awaited.

Detailed answer

Create the tasks first, then await Task.WhenAll to allow concurrency where the underlying work supports it. Do not use Task.WaitAll in asynchronous application code because it blocks a thread. If tasks are created with LINQ, materialize the sequence with ToArray or ToList so all operations actually start before awaiting.

For example, start var products = LoadProductsAsync(); and var prices = LoadPricesAsync();, then await Task.WhenAll(products, prices) before combining their results. This allows independent I/O to overlap without hiding either task’s failure.

Sources