Waiting on tasks where number of tasks could vary

213 views Asked by At

I need to wait for one or more tasks to complete. The number of tasks that I need to wait on depends upon a few conditions. So my code is something like below.

Task[] tasks = new Task[2];

if (condition1 == true)
{
    tasks[0] = Task.Factory.StartNew(() => MyMethod1());
}

if (condition2 == true)
{
    tasks[1] = Task.Factory.StartNew(() => MyMethod2());
}

Task.WaitAll(tasks);

Let's say condition2 is false, so there is only one item in the tasks array. Will the WaitAll only wait for tasks[0] in this case?

2

There are 2 answers

1
Michael Liu On BEST ANSWER

Both Task.WaitAll and Task.WhenAll throw ArgumentException if any element of the tasks array is null.

To avoid the exception, you can add your tasks to a List<Task> instead:

List<Task> tasks = new List<Task>();

if (condition1)
{
    tasks.Add(Task.Factory.StartNew(() => MyMethod1()));
}

if (condition2)
{
    tasks.Add(Task.Factory.StartNew(() => MyMethod2()));
}

Task.WaitAll(tasks.ToArray());
0
Philip Stuyck On

It is better to use WhenAll which supports IEnumerable as parameter. This allows you to use a different container than an array and will avoid the exception in the first place.

List<Task> tasks= new List<Task>();

if (condition1)
{
    tasks.Add(Task.Factory.StartNew(() => MyMethod1()));
}

if (condition2)
{ 
    tasks.Add(Task.Factory.StartNew(() => MyMethod2()));
}

await Task.WhenAll(tasks);

See also WaitAll vs WhenAll