What if you could finish many jobs at once instead of waiting forever for each to end?
Why Task.WhenAll for parallel execution in C Sharp (C#)? - Purpose & Use Cases
Imagine you need to download multiple files one by one. You start the first download, wait for it to finish, then start the second, and so on. This takes a long time and wastes your computer's power.
Doing tasks one after another is slow and boring. If one task takes longer, everything waits. Also, writing code to manage many tasks manually is confusing and easy to mess up.
Task.WhenAll lets you start many tasks at the same time and wait for all of them to finish together. This saves time and makes your code cleaner and easier to understand.
await task1; await task2; await task3;
await Task.WhenAll(task1, task2, task3);
You can run many jobs at once, making your programs faster and more efficient without complicated code.
Downloading images from the internet simultaneously instead of waiting for each one to finish before starting the next.
Running tasks one by one wastes time and resources.
Task.WhenAll runs tasks in parallel and waits for all to finish.
This makes your code simpler and your program faster.
Practice
What does Task.WhenAll do in C#?
Solution
Step 1: Understand Task.WhenAll purpose
Task.WhenAll is designed to run multiple tasks in parallel and wait until all complete.Step 2: Compare options with behavior
Only Runs multiple tasks at the same time and waits for all to finish correctly describes running tasks simultaneously and waiting for all to finish.Final Answer:
Runs multiple tasks at the same time and waits for all to finish -> Option AQuick Check:
Task.WhenAll = parallel run and wait [OK]
- Thinking tasks run one by one
- Believing it cancels tasks
- Assuming it runs only one task
Which of the following is the correct syntax to wait for multiple tasks task1 and task2 to complete using Task.WhenAll?
?
Solution
Step 1: Recall Task.WhenAll syntax
Task.WhenAll takes tasks as parameters inside parentheses and returns a Task to await.Step 2: Check each option's syntax
Only await Task.WhenAll(task1, task2); uses parentheses correctly and awaits the combined task.Final Answer:
await Task.WhenAll(task1, task2); -> Option BQuick Check:
Correct syntax uses parentheses () [OK]
- Using square brackets [] instead of parentheses
- Using curly braces {} incorrectly
- Using angle brackets <> wrongly
What will be the output of this C# code?
using System;
using System.Threading.Tasks;
class Program {
static async Task Main() {
Task<int> t1 = Task.FromResult(5);
Task<int> t2 = Task.FromResult(10);
int[] results = await Task.WhenAll(t1, t2);
Console.WriteLine(string.Join(",", results));
}
}Solution
Step 1: Understand Task.FromResult and Task.WhenAll
Task.FromResult creates completed tasks with values 5 and 10. Task.WhenAll waits and returns results as an int array.Step 2: Check output of Console.WriteLine
Results array contains [5, 10], so joining with comma prints "5,10".Final Answer:
5,10 -> Option CQuick Check:
Task.WhenAll returns array of results [OK]
- Assuming results are reversed
- Expecting a string instead of array
- Thinking code causes error
Identify the error in this code snippet using Task.WhenAll:
async Task RunTasks() {
var t1 = Task.Delay(1000);
var t2 = Task.Delay(2000);
Task.WhenAll(t1, t2);
Console.WriteLine("Done");
}Solution
Step 1: Check usage of Task.WhenAll
Task.WhenAll returns a Task that must be awaited to wait for all tasks to finish.Step 2: Identify missing await
Code calls Task.WhenAll but does not await it, so "Done" prints immediately before delays finish.Final Answer:
Missing await before Task.WhenAll, so tasks run but not waited -> Option AQuick Check:
Always await Task.WhenAll to wait [OK]
- Not awaiting Task.WhenAll
- Thinking Task.Delay can't be used
- Misplacing Console.WriteLine
You want to run three tasks that return strings: Task<string> t1, t2, and t3. You want to get all results as a list of strings after all finish. Which code correctly does this?
Solution
Step 1: Understand Task.WhenAll return type
Task.WhenAll returns a string array when awaited on Task<string> tasks.Step 2: Convert array to list correctly
var results = await Task.WhenAll(t1, t2, t3); var list = results.ToList(); awaits Task.WhenAll, then converts the array to a list using ToList(), which is correct.Final Answer:
var results = await Task.WhenAll(t1, t2, t3); var list = results.ToList(); -> Option DQuick Check:
Await Task.WhenAll then convert array to list [OK]
- Trying to call ToList() before awaiting
- Using .Result causing blocking
- Passing list directly without awaiting
Ready to prove it?
Pass the test and this pattern moves unknown
