73

I start a few parallel tasks, like this:

var tasks =
    Enumerable.Range(1, 500)
    .Select(i => Task.Factory.StartNew<int>(ProduceSomeMagicIntValue))
    .ToArray();

and then join them with

Task.WaitAll(tasks);

On this last line I get a blue squiggly marker under tasks, with a warning message:

Co-variant array conversion from Task[] to Task[] 
can cause run-time exception on write operation.

I understand why I get this message, but is there a way around that? (for example, like a generic version of Task.WaitAll()?)

5
  • 3
    In this case, the conversion is safe, because WaitAll() won't write to the array. Is there any reason why you want to avoid it?
    – svick
    Commented Apr 6, 2012 at 13:58
  • 17
    Also, .Net 4.5 will contain Task.WhenAll() that returns a single Task that completes when all the Tasks in the collection complete. And it also has a generic version and it works on any IEnumerable<T> of Tasks.
    – svick
    Commented Apr 6, 2012 at 14:02
  • @svick thx for the tip. looks like they renamed what you're talking about to WhenAll so you can just say 'await Task.WhenAll(task1, task2);' Commented Sep 10, 2012 at 19:34
  • any chance you'd consider accepting a different answer? I personally found others more worthy than the one you've chosen as "accepted". Commented Nov 21, 2018 at 18:34
  • 1
    @Quibblesome Done. Commented Nov 22, 2018 at 7:54

4 Answers 4

34

A generic method of Task.WaitAll would imply that all Tasks would have to return the same type which would be extremely limited usefulness. Writting something like that could be done manually (see Bas Brekelmans answer), but this wont allow ContinueWith or cancellation without alot of work.

A simple solution if you aren't using the array for anything else is

  .ToArray<Task>();
2
  • 1
    +1 that's a possibility, but in my case, I actually use the array to get the int results: var results = tasks.Select(task => task.Result) Commented Apr 6, 2012 at 13:45
  • 1
    @W0lf then simply do an extra .ToArray<Task>() before you pass it to Task.WaitAll, any possible assignment to this copied Task[] can't cause a runtime exception
    – MerickOWA
    Commented Apr 6, 2012 at 14:04
30

I'm pretty sure it's a safe operation even with the warning, but if you really wanted to get around it a better option than creating your own implementation would be just to convert your tasks parameter into the type it wants:

Task.WaitAll(tasks.Cast<Task>().ToArray())

That kills the blue squiggles for me, lets me keep my tasks variable generic and doesn't force me to create a whole lot of new scary code that's ultimately unnecessary.

2
  • 1
    This worked perfectly for me. Tested it both with tasks that ran to completion, and tasks that timed out, using var ifAllTasksRanToCompletion = Task.WaitAll(tasks.Cast<Task>().ToArray(), timeout: TimeSpan.FromSeconds(20));.
    – Contango
    Commented Sep 25, 2014 at 15:16
  • But this may be slower (when ran in parallel, PLINQ) due the .ToArray() as said here: learn.microsoft.com/en-us/dotnet/standard/parallel-programming/… Commented Nov 9, 2022 at 13:42
21

A BETTER AND SIMPLER ANSWER

Actually there IS a similar generic overload:

Task all = Task.WhenAll(tasks)

This is different in this it returns a Task that will complete after all tasks completed. so you can use await on it, or Wait(), whatever you want.

Look at the signature:

Overloads

--------- NON GENERIC OVERLOADS --------------

WhenAll(IEnumerable<Task>) Creates a task that will complete when all of the Task objects in an enumerable collection have completed.

WhenAll(Task[]) Creates a task that will complete when all of the Task objects in an array have completed.

--------- GENERIC OVERLOADS --------------

WhenAll<TResult>(IEnumerable<Task<TResult>>) Creates a task that will complete when all of the Task<TResult> objects in an enumerable collection have completed.

WhenAll<TResult>(Task<TResult>[]) Creates a task that will complete when all of the Task<TResult> objects in an array have completed.

6

You can create an extension method to do this.

I do not know the exact implementation of WaitAll, but we can assume it waits for every item to complete:

static class TaskExtensions
{
    public static void WaitAll<T>(this Task<T>[] tasks)
    {
        foreach (var item in tasks)
        {
            item.Wait();
        }
    }
}

Then call, from your current code:

tasks.WaitAll();

Edit

The actual implementation is a bit more complex. I have omitted the code from this answer because it is fairly long.

http://pastebin.com/u30PmrdS

You can modify this to support generic tasks.

4
  • 2
    The "actual implementation" is quite non-trivial. Not to mention, you loose out (or need to re-code) if the .NET Framework implementation has any bug fixes or performance improvements. The solutions from @MerickOWA and DMac the Destroyer (in that order) resolve the OP's issue very simply.
    – Mike
    Commented Jul 20, 2017 at 7:13
  • 2
    This is really obviously wrong - you're running your tasks serially here, which isn't what the real WaitAll does.
    – Mark Amery
    Commented Aug 31, 2017 at 12:12
  • 1
    @MarkAmery this assumes the tasks are already dispatched when they are fed into this method.
    – Bas
    Commented Aug 31, 2017 at 17:30
  • 2
    If people insist on building their own (instead of using one of the other solutions that convert the generic collection into an array of Task) this is a better source, with full comments and links to all of the internal code that you will also need to duplicate to make it work: referencesource.microsoft.com/#mscorlib/system/threading/Tasks/…
    – Mike
    Commented Sep 7, 2017 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.