5

I have a pretty simple looking problem, but I can't seem to find the answer anywhere.

Given I have several arrays of tasks of different types e.g.

Task<Dog>[] dogTasks = GetDogTasks();
Task<Cat>[] catTasks = GetCatTasks();
Task<Fish>[] fishTasks = GetFishTasks();

What would be the best way to WaitAll() for these tasks?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Phil Pill
  • 303
  • 2
  • 14
  • @PatrickHofman I get the error `The best overloaded method match for 'System.Threading.Tasks.Task.WaitAll(params System.Threading.Tasks.Task[])' has some invalid arguments` - `Argument 1: cannot convert from 'System.Threading.Tasks.Task[]' to 'System.Threading.Tasks.Task'` for each of the values I try to wait for. – Phil Pill Dec 03 '15 at 10:45
  • Sorry, missed it was an array. See the duplicate to see how to construct a new array of all tasks. – Patrick Hofman Dec 03 '15 at 10:49
  • No worries. I tried the other solution already - it doesn't work for differing types. My question is not a duplicate of the other. – Phil Pill Dec 03 '15 at 10:50

1 Answers1

5

WaitAll should solve your problem. You have arrays of tasks so you should create one array from them:

var tasks = new List<Task>();
tasks.AddRange(dogTasks);
tasks.AddRange(catTasks);
tasks.AddRange(fishTasks);
Task.WaitAll(tasks.ToArray());
Sergii Zhevzhyk
  • 4,074
  • 22
  • 28