Is there an equivalent to 'continue' in a Parallel.ForEach?

C#ForeachParallel Processing

C# Problem Overview


I am porting some code to Parallel.ForEach and got an error with a continue I have in the code. Is there something equivalent I can use in a Parallel.ForEach functionally equivalent to continue in a foreach loop?

Parallel.ForEach(items, parallelOptions, item =>
{
    if (!isTrue)
	    continue;
});

C# Solutions


Solution 1 - C#

return;

(the body is just a function called for each item)

Solution 2 - C#

When you converted your loop into a compatible definition for the Parallel.Foreach logic, you ended up making the statement body a lambda. Well, that is an action that gets called by the Parallel function.

So, replace continue with return, and break with Stop() or Break() statements.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn EgbertView Question on Stackoverflow
Solution 1 - C#daveView Answer on Stackoverflow
Solution 2 - C#TaranView Answer on Stackoverflow