If you want to stop after executing the current executable iteration (but iterating until the current WILL, i.e. iteration with a lower index = i)
Parallel.For(0, 100, (i, s) => { s.Break(); });
or if you want to stop after the current and intercoats before (in terms of index = i), you should also stop
Parallel.For(0, 100, (i, s) => { s.Stop(); });
but in cases of BOTH, itβs good practice to check if work should be interrupted if iteration can take some time
s.ShouldExitCurrentIteration
more details here Parallel.For Method (Int32, Int32, Action (Int32, ParallelLoopState))
Lasse espeholt
source share