i have an extension method for IEnumerable which then iterates through the collection, doing its business, and then returns an new IEnumerable.
I have tried to use PLINQ using .AsParallel().ForAll() which speeds up the iterations significantly (which of course it should do) however when the collection is returned, there are often a few objects in that collection that are null.
I’m assuming this may be because it is returning the collection before all the ‘business’ has a chance to complete? if i debug and put in a breakpoint, there are no nulls.
is there some sort of ‘wait for this operation to be completed’ method that i should be using?
EDIT: to be a bit clearer, there is business logic in the forall, modifying properties etc. it’s necessary to have an action looped, rather than simply selecting something.
The answer depends on what you mean by returning, because the
ForAllmethod doesn’t return anything. It invokes the delegate you specify in parallel for all elements of the collection. I would suppose that your code looks like this:The
ForAllmethod doesn’t wait for all delegates to complete, somore codecan execute before all the delegates complete (and you also need to be careful in thestore result somewherebit, because it may run concurrently for multiple delegates!)I think the code could be rewritten more elegantly using the
Selectmethod:In this case, the delegate simply returns the result. The
Wheremethod collects all the results and when you iterate over the returnedIEnumerable, it guarantees that all delegates finished calculating.