Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a generic Task.WaitAll?

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()?)

like image 363
Cristian Lupascu Avatar asked Apr 06 '12 13:04

Cristian Lupascu


People also ask

What is Task WaitAll?

WaitAll(Task[], TimeSpan) Waits for all of the provided cancellable Task objects to complete execution within a specified time interval. WaitAll(Task[], Int32, CancellationToken) Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled.

What is the difference between task WaitAll and task WhenAll?

The Task. WaitAll blocks the current thread until all other tasks have completed execution. The Task. WhenAll method is used to create a task that will complete if and only if all the other tasks have completed.


4 Answers

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>();
like image 149
MerickOWA Avatar answered Oct 12 '22 00:10

MerickOWA


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.

like image 23
DMac the Destroyer Avatar answered Oct 11 '22 22:10

DMac the Destroyer


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.

like image 19
Yitzchak Avatar answered Oct 11 '22 23:10

Yitzchak


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.

like image 6
Bas Avatar answered Oct 12 '22 00:10

Bas