2

When I read the MSDN documentation on Task.WaitAll(params Task[] tasks) it says it throws an ArgumentException when:

The tasks argument contains a null element.

-or-

The tasks argument is an empty array.

But when I call Task.WaitAll(new Task[0]); it passes on without any exception.

Am I misunderstanding the documentation, or is this just an error (or outdated info)?

I understand Task.WhenAll(new Task[0]) does the same, except returning a task of course. This feature was sometimes used to get a completed task, before Task.CompletedTask was introduced in .NET 4.6.

I am actually glad that Task.WaitAll() seems to behave this way, so I don't have to treat an empty array as a special case, but I still want to make sure that I understood it right.

I am running my application on 4.5.2 by the way. But I just made a new solution targeted to 4.6.1 and it behaves the same way.

Community
  • 1
  • 1
Aske B.
  • 6,419
  • 8
  • 35
  • 62
  • 2
    The documentation certainly changed between [VS 10](https://msdn.microsoft.com/en-us/library/dd270695(v=vs.100).aspx) and [VS 11](https://msdn.microsoft.com/en-us/library/dd270695(v=vs.110).aspx) - specifically it adds the `tasks argument is an empty array` text - interesting. My money is documentation error... – RB. Feb 22 '17 at 11:13

1 Answers1

2

Its seems to be a wrong documentation. According to source code (Can be found here or via Resharper) ArgumentException raised only when:

Argument is null:

5043     if (tasks == null)
5044     {
5045        throw new ArgumentNullException("tasks");
5046     }

Or one of array's element is `null':

5070    // Collects incomplete tasks in "waitedOnTaskList"
5071    for (int i = tasks.Length - 1; i >= 0; i--)
5072    {
5073        Task task = tasks[i];
5074
5075        if (task == null)
5076        {
5077            throw new ArgumentException(Environment.GetResourceString("Task_WaitMulti_NullTask"), "tasks");
5078        }

I cant find any checks for empty array in code.

PS: I was looking at .NET Framework 4.6.2 source code

Community
  • 1
  • 1
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69