What if you could instantly react to the fastest task without messy code?
Why Task.WhenAny for first completion in C Sharp (C#)? - Purpose & Use Cases
Imagine you start several tasks at once, like downloading files or fetching data from different servers. You want to proceed as soon as the first task finishes, but checking each one manually is tricky.
Manually checking each task's completion means writing lots of code to poll or wait for each task. This is slow, messy, and easy to make mistakes, especially when tasks finish at unpredictable times.
Task.WhenAny lets you wait for the first task to finish without extra checks. It simplifies your code and makes it efficient by immediately giving you the completed task to work with.
while(true) { if(task1.IsCompleted) break; if(task2.IsCompleted) break; Thread.Sleep(100); }
var firstDone = await Task.WhenAny(task1, task2);
You can react instantly to the first completed task, making your program faster and cleaner.
When loading data from multiple sources, you can display the fastest response to the user without waiting for all sources to finish.
Manual checking of tasks is slow and error-prone.
Task.WhenAny waits for the first task to finish efficiently.
This makes your code simpler and more responsive.
Practice
Task.WhenAny do in C#?Solution
Step 1: Understand Task.WhenAny purpose
Task.WhenAny waits for the first task to finish among many tasks.Step 2: Compare with other options
Unlike Task.WhenAll, it does not wait for all tasks, nor does it cancel tasks or create infinite tasks.Final Answer:
It waits for the first task to complete among multiple tasks. -> Option DQuick Check:
Task.WhenAny = first task completion [OK]
- Confusing Task.WhenAny with Task.WhenAll
- Thinking it cancels other tasks
- Assuming it waits for all tasks
Task.WhenAny with two tasks task1 and task2?Solution
Step 1: Check correct method call syntax
Task.WhenAny accepts params array of tasks, so calling with (task1, task2) is correct.Step 2: Identify invalid syntax
Square brackets, curly braces, or missing parentheses cause syntax errors.Final Answer:
var first = await Task.WhenAny(task1, task2); -> Option AQuick Check:
Correct method call uses parentheses and comma-separated tasks [OK]
- Using square brackets instead of parentheses
- Using curly braces instead of parentheses
- Omitting parentheses around arguments
var task1 = Task.Delay(3000).ContinueWith(_ => "Task1 done"); var task2 = Task.Delay(1000).ContinueWith(_ => "Task2 done"); var first = await Task.WhenAny(task1, task2); Console.WriteLine(await first);
Solution
Step 1: Analyze task delays
task1 completes after 3000ms, task2 after 1000ms, so task2 finishes first.Step 2: Task.WhenAny returns first completed task
first will be task2, so awaiting it prints "Task2 done".Final Answer:
Task2 done -> Option AQuick Check:
First completed task result = Task2 done [OK]
- Assuming task1 finishes first because it is listed first
- Expecting both tasks to print
- Thinking program hangs waiting for all tasks
Task.WhenAny:
var task1 = Task.Delay(2000); var task2 = Task.Delay(1000); var first = Task.WhenAny(task1, task2); Console.WriteLine(await first.Result);
Solution
Step 1: Check how Task.WhenAny is awaited
Task.WhenAny returns a Task<Task>, so it must be awaited to get the completed task.Step 2: Identify misuse of Result property
Accessing first.Result without awaiting first causes incorrect behavior or deadlock.Final Answer:
Task.WhenAny should be awaited before accessing Result. -> Option BQuick Check:
Await Task.WhenAny before accessing completed task [OK]
- Accessing Result property without await
- Confusing Task<Task> with Task
- Ignoring async/await pattern
taskA (2s delay), taskB (1s delay), and taskC (3s delay). You want to print the result of the first completed task and then cancel the other two tasks. Which code snippet correctly achieves this?Solution
Step 1: Use CancellationTokenSource to cancel tasks
Create a CancellationTokenSource and pass its token to all tasks so they can be cancelled.Step 2: Await Task.WhenAny and cancel remaining tasks
Await Task.WhenAny to get first completed task, then call cts.Cancel() to stop others, finally print the result.Final Answer:
Use CancellationTokenSource, await Task.WhenAny, then cancel and print. -> Option CQuick Check:
Cancel tasks after first completion using CancellationTokenSource [OK]
- Not passing cancellation token to tasks
- Calling Cancel before awaiting Task.WhenAny
- Using .Result instead of await causing deadlocks
Ready to prove it?
Pass the test and this pattern moves unknown
