19

I have an object that returns a System.Threading.Tasks.Task:

public class MyClass 
{
    public Task GetTask(object state, CancellationToken cancellationToken)
    {
        return new Task(Execute, state, cancellationToken);
    }

    public void Execute(object context)
    {
        //do stuff
    }
}

Elsewhere I have a List<MyClass>, so I do the following to get a List<Task>:

var myTaskList = myClassList.Select(p => p.GetTask(null, cancellationToken)).ToList();

Now that I have the List<Task>, how can I start them all in parallel? Is there a more concise way to code this?

Thanks!

2 Answers 2

29

What do you mean by "start them in parallel"? When you run the Task, it executes on another thread, so you probably mean simply:

foreach(var task in myTaskList)
{
    task.Start();
}

But if you have so many of them that you want to move the starting logic to another thread, you can either call the above code in another thread/task (I'm using List<T>.ForEach for shorter code).

Task.Factory.StartNew(() => myTaskList.ForEach(task => task.Start()));

Or you can use TPL's Parallel.ForEach. That would still block the executing thread until all the Tasks are started, but it will execute the start action on an internal threadpool, so for large numbers of items and some free CPU cores/threads, it might speed up the starting considerably.

Parallel.ForEach(myTaskList, task => task.Start());
5
  • 2
    Thank you, the Parallel.ForEach() was what I was looking for. Dec 12, 2012 at 10:50
  • Parallel.ForEach will still block the current thread until it has completed all of its iterations.
    – Dave New
    Dec 13, 2012 at 8:28
  • You are right - the question was quite unclear, but so is my answer. Truly parallel way is to execute (any) task.Start() logic in a separate thread (i.e. Thread object, Task with LongRunning creation option, Parallel.Invoke etc.) Dec 13, 2012 at 8:39
  • A nitpick on an otherwise great answer. There's no such thing as "LINQ's ForEach" in the middle example. The ForEach you're using is a method on List<T> and Enumerable does not provide a ForEach method. (For good reason: for each does not return a value to which you could chain further computation and so in most cases you could just foreach.)
    – Jesper
    Dec 13, 2012 at 8:54
  • True, it's defined for List<T> only and uses a for internally, but I noticed it together with LINQ, so I tend to call it that way. I'll clarify my answer. Dec 13, 2012 at 8:57
7

I might be misunderstanding your question, but isn't it just to call Start on every task?

foreach(Task task in myTaskList) 
{
    task.Start();
}

Waiting for all the tasks to finish:

Task.WaitAll(myTaskList.ToArray());
3
  • 3
    Or, via Linq: myTaskList.ForEach(task => task.Start()); Dec 12, 2012 at 10:37
  • I was unable to run Parallel.Foreach on my Task because it was a promise-styled Task; this Task.WaitAll worked for me instead. Sep 21, 2017 at 16:32
  • 1
    @sphair "Task.WaitAll(myTaskList.ToArray());" gives me "Cannot await 'void'"
    – Janneman96
    Feb 24, 2022 at 14:12

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.