Task.WaitAny accept Task <T> instead of Task []

I have a set of common tasks that are marked for execution. When and when the task is completed (using Task.WaitAny), I add it to ObservableCollection.

But the problem is in the line Task.WaitAny(...), which says that converting the covariant array from the <MyType> [] task to the [] task may cause a run-time exception during a write operation.

I am quite aware of what this exception means and why it complains at this stage.

Question: Is there any general version Task.WaitAny()that can take Task<T>as a parameter instead Task[].

Thanks in advance.

code

+4
2

Task.WhenAny

public static Task<Task<TResult>> WhenAny<TResult>(IEnumerable<Task<TResult>> tasks);
public static Task<Task<TResult>> WhenAny<TResult>(params Task<TResult>[] tasks);

await, .

+2

Task.WhenAny, ,

var result = new ObservableCollection<LenderScoreCard>();
var parallelScoreList = parallelScore.ToList();
while (parallelScoreList.Count != 0)
{
    var completedTask = Task.WhenAny(parallelScoreList).Result;
    result.Add(completedTask.Result);
    parallelScoreList.Remove(completedTask);
}
+2

Source: https://habr.com/ru/post/1610634/


All Articles