Say task1, task2, task3 are tasks that I start at the beginning of the function, what does
await Task.WhenAny(task1,task2,task3); return if more than one task has finished before the code reaches that await statement? Is it a random task? The first from the arguments that finished? A list of the tasks that have completed?
It does not matter by the way, I'm just wondering if it's uncertain behavior or if there's a priority
Let's start with this super simple application:
Here we have three tasks and all will complete in a second. If you run this application multiple times then
res.Idwill be one of the three tasks depending the task scheduling.Now, let's extend it with tasks that are completed before reaching the
Task.WhenAnyIn my experiments
res.Idwas alwaystask4.Id.The above example is a bit bias. So, lets change the
task4andtask5toTask.Delay(10);.In this case
res.Idwill betask4.Idortask5.Iddepending on scheduling.No, it does not matter whether you add
task4before or aftertask5to the parameter list ofTask.WhenAny.No. If you need that a simple Linq query can return that