Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I follow-up a Parallel.ForEach on completion?

I want to take a set of objects and run a routine on all of them. The order doesn't matter and they are each independent operations, so I thought I would call Parallel.ForEach on the collection. But I want to follow-up the whole thing once complete.

Where is the ContinueWith equivalent or an overload of ForEach that takes another action/Task to run on completion? Am I stuck polling the ParallelLoopResult.IsCompleted value until it comes back true?

The ContinueWhenAll method always expects an array of Tasks. Should I instead project the set of objects into new Tasks for each? How would I then start an array of Tasks all at once and in parallel?

This question is similar, but concerns the older 3.5 TPL Extensions I believe. I'm open to solutions outside of the Task Parallel Library if need be.

like image 204
Sean Hanley Avatar asked Oct 04 '11 18:10

Sean Hanley


1 Answers

Parallel.ForEach blocks until it's finished, so you can just do whatever you need to after the method call.

like image 149
Jon Skeet Avatar answered Oct 19 '22 02:10

Jon Skeet