I'm trying to implement a task based interleaving pattern similar to below.
This logic is in a library and so I'm trying to make use of ConfigureAwait(false) as I don't see a reason that the task must resume in the same context.
In the below example line 3 will fail as Task1 and Task2 are not actually Tasks but instead ConfiguredTaskAwaitable Further using these Task1 and Task2 directly in Task.WhenAny(Task1,Task2) it will also fail as it only accepts Tasks not the ConfiguredTaskAwaitable class.
var Task1 = await GetTask1Async().ConfigureAwait(false);
var Task2 = await GetTask2Async().ConfigureAwait(false);
var tasks = new List<Task<string>>{Task1 ,Task2};
while(tasks.Count > 0)
{
try
{
Task<Bitmap> readyTask = await Task.WhenAny(tasks);
tasks.Remove(readyTask);
string result = await readyTask ;
ProcessTask(result);
}
catch{}
}
Is there a reason why I shouldn't use ConfigureAwait(false) with Task.WhenAny()? Or is there some step I am missing that I should apply to the output of the ConfigureAwait() before passing it to the Task.WhenAny()?