0
0
C Sharp (C#)programming~10 mins

Task.WhenAny for first completion in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task.WhenAny for first completion
Start multiple tasks
Wait for any task to complete
Identify first completed task
Process result of first completed task
Optionally wait for others or continue
Start several tasks, wait until the first one finishes, then handle its result before continuing.
Execution Sample
C Sharp (C#)
var task1 = Task.Delay(3000);
var task2 = Task.Delay(1000);
var first = await Task.WhenAny(task1, task2);
Console.WriteLine($"First finished: {first.Id}");
Starts two delay tasks and prints which one finishes first.
Execution Table
StepActionTask1 StatusTask2 StatusTask CompletedOutput
1Start task1 (3s delay)RunningNot startedNone
2Start task2 (1s delay)RunningRunningNone
3Wait for any task to completeRunningRunningNone
4After 1 second, task2 completesRunningCompletedtask2First finished: task2.Id
5Continue after first completionRunningCompletedtask2First finished: task2.Id
6Optionally wait for task1CompletedCompletedtask2
💡 Task2 completes first after 1 second, so Task.WhenAny returns task2.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
task1.StatusNot startedRunningRunningCompleted
task2.StatusNot startedRunningCompletedCompleted
firstnullnulltask2task2
Key Moments - 3 Insights
Why does Task.WhenAny return the task that finished first, not the result?
Task.WhenAny returns the Task object that completed first, not its result value. You can then await that task to get its result. See execution_table step 4 where 'first' is assigned the completed task.
What happens to the other tasks after Task.WhenAny completes?
Other tasks keep running unless you explicitly wait or cancel them. In the example, task1 continues running after task2 finishes (see execution_table steps 4 and 5).
Can Task.WhenAny complete if multiple tasks finish at the same time?
Yes, but it returns one of the completed tasks (usually the first detected). The exact one is not guaranteed. This is why you check which task completed by inspecting the returned task.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the status of task1 at step 4?
ARunning
BCompleted
CNot started
DFaulted
💡 Hint
Check the 'Task1 Status' column at step 4 in the execution_table.
At which step does Task.WhenAny return the first completed task?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look for when 'Task Completed' column shows a task and output is produced.
If task2 had a 5 second delay instead of 1 second, which task would Task.WhenAny return first?
ANeither, it would wait forever
Btask1
Ctask2
DBoth at the same time
💡 Hint
Refer to variable_tracker to see which task finishes earlier based on delay times.
Concept Snapshot
Task.WhenAny(tasks...) waits for the first task to finish.
It returns the completed Task object, not its result.
You can await the returned task to get its result.
Other tasks keep running unless awaited or cancelled.
Useful to react as soon as any task completes.
Full Transcript
This example shows how Task.WhenAny works by starting two delay tasks. Task1 delays 3 seconds, task2 delays 1 second. The program waits for the first task to complete using Task.WhenAny. After 1 second, task2 finishes first, so Task.WhenAny returns task2. The program then prints the ID of the first finished task. Task1 continues running until it finishes. This demonstrates that Task.WhenAny returns the first completed task object, allowing you to handle its result or continue processing.