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

Task.WhenAny for first completion in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does Task.WhenAny do in C#?

Task.WhenAny waits for any one of a set of tasks to complete and returns the first completed task.

Click to reveal answer
beginner
How do you get the result of the first completed task when using Task.WhenAny?

After Task.WhenAny returns, you can access the Result property of the returned task if it is a Task<T>.

Click to reveal answer
beginner
True or False: Task.WhenAny waits for all tasks to complete before returning.

False. Task.WhenAny returns as soon as any one task completes.

Click to reveal answer
intermediate
What type does Task.WhenAny return?

It returns a Task<Task> representing the first completed task.

Click to reveal answer
intermediate
Why might you use Task.WhenAny instead of Task.WhenAll?

You use Task.WhenAny when you want to proceed as soon as the first task finishes, instead of waiting for all tasks.

Click to reveal answer
What does Task.WhenAny return?
AA list of all task results
BAll tasks after completion
CThe last completed task
DThe first completed task
If you want to wait for all tasks to finish, which method should you use?
ATask.WhenAny
BTask.WhenAll
CTask.WaitAny
DTask.WaitAll
After Task.WhenAny completes, how do you get the result of the first finished task if it returns a value?
AUse <code>await</code> on the returned task and access <code>Result</code>
BUse <code>Task.WaitAll</code>
CUse <code>Task.Result</code> on the original tasks
DYou cannot get the result
Which of these is a valid use case for Task.WhenAny?
AStart multiple downloads and process the first one that finishes
BWait for all downloads to finish before processing
CCancel all tasks immediately
DRun tasks sequentially
What happens to the other tasks after Task.WhenAny returns?
AThey complete immediately
BThey are automatically cancelled
CThey continue running unless cancelled
DThey throw exceptions
Explain how Task.WhenAny works and when you would use it.
Think about situations where you want to proceed as soon as one task finishes.
You got /4 concepts.
    Describe the difference between Task.WhenAny and Task.WhenAll.
    Compare waiting for one task vs. waiting for all tasks.
    You got /4 concepts.