Answered by:
ParallelLoopState.Break() and ParallelLoopState.Stop()
Question

User1904516115 posted
What is the difference between ParallelLoopState.Break() and ParallelLoopState.Stop()? Please give an example
Monday, February 5, 2018 5:20 PM
Answers

User832373396 posted
Hi vinodkpasi,
Sir, here is the testing example
 ParallelLoopState.Break()
[TestMethod] public void TestMethod16() { var result = Parallel.For(1, 10, (i, state) => { if (i > 3) { Debug.WriteLine("Break in iteration {0}", i); state.Break(); } Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine("done"); }
Output
Completed iteration 1 Completed iteration 3 Break in iteration 5 Completed iteration 5 Completed iteration 2 Break in iteration 7 Completed iteration 7 Break in iteration 9 Break in iteration 4 Completed iteration 4 Completed iteration 9 done
 ParallelLoopState.Stop()
[TestMethod] public void TestMethod17() { var result = Parallel.For(1,10, (i, state) => { if (i > 3) { Debug.WriteLine("Stop in iteration {0}", i); state.Stop(); return; } Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine("done" ); }
Output
Completed iteration 1 Completed iteration 3 Stop in iteration 4 Stop in iteration 7 Stop in iteration 9 Stop in iteration 5 Completed iteration 2 done
It seems that it is a bit different between them.
Next
Test example
1 ParallelLoopState.Break() 2 ParallelLoopState.Stop() [TestMethod] public void TestMethod16() { var c = 0; var result = Parallel.For(1, 100, (i, state) => { //if (state.ShouldExitCurrentIteration) //{ // if (state.LowestBreakIteration < i) // return; //} if (i>50) { Debug.WriteLine("Break in iteration {0}", i); state.Break(); } c++; Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine(c); Debug.WriteLine("done"); }
[TestMethod] public void TestMethod17() {
var c = 0; var result = Parallel.For(1,100, (i, state) => {
if (i > 50) { Debug.WriteLine("Stop in iteration {0}", i); state.Stop();
//return ; } //if (state.IsStopped) //{ // return; //} c++; Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine(c); Debug.WriteLine("done" ); } }
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Completed iteration 29
Completed iteration 30
Completed iteration 2
Completed iteration 49
Break in iteration 97
Break in iteration 73
Completed iteration 73
Completed iteration 97
Completed iteration 31
Completed iteration 50
Break in iteration 51
Completed iteration 51
Completed iteration 3
Completed iteration 4
Completed iteration 5
Completed iteration 6
Completed iteration 7
Completed iteration 8
Completed iteration 9
Completed iteration 10
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 38
Completed iteration 11
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 17
Completed iteration 18
Completed iteration 43
Completed iteration 19
Completed iteration 44
Completed iteration 45
Completed iteration 46
Completed iteration 47
Completed iteration 48
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
53
Done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Completed iteration 29
Stop in iteration 73
Completed iteration 49
Completed iteration 2
Completed iteration 73
Stop in iteration 97
Completed iteration 97
Completed iteration 30
11
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Break in iteration 73
Completed iteration 73
Completed iteration 29
Completed iteration 49
Completed iteration 2
Completed iteration 3
Break in iteration 97
Completed iteration 97
Completed iteration 4
Completed iteration 5
Completed iteration 50
Break in iteration 51
Completed iteration 30
Completed iteration 31
Completed iteration 6
Completed iteration 7
Completed iteration 8
Completed iteration 9
Completed iteration 10
Completed iteration 11
Completed iteration 51
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 17
Completed iteration 18
Completed iteration 19
Completed iteration 38
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 43
Completed iteration 44
Completed iteration 45
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
Completed iteration 46
Completed iteration 47
Completed iteration 48
53
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Completed iteration 29
Stop in iteration 73
Completed iteration 73
Stop in iteration 97
Completed iteration 2
Completed iteration 49
Completed iteration 30
Completed iteration 97
11
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Break in iteration 73
Completed iteration 73
Completed iteration 29
Completed iteration 49
Completed iteration 2
Completed iteration 3
Break in iteration 97
Completed iteration 97
Completed iteration 4
Completed iteration 5
Completed iteration 50
Break in iteration 51
Completed iteration 30
Completed iteration 31
Completed iteration 6
Completed iteration 7
Completed iteration 8
Completed iteration 9
Completed iteration 10
Completed iteration 11
Completed iteration 51
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 17
Completed iteration 18
Completed iteration 19
Completed iteration 38
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 43
Completed iteration 44
Completed iteration 45
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
Completed iteration 46
Completed iteration 47
Completed iteration 48
53
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Stop in iteration 73
Completed iteration 73
Completed iteration 49
Stop in iteration 97
Completed iteration 2
Completed iteration 29
Completed iteration 97
10
done
Elapsed time
Elapsed time
about 303ms
about 94ms
We could see that if I remove the specified code for them, they are almost same. but from the test result, we could view the difference that if we use stop, it will stop reusing more resources quickly. and as the official document tells, if we want to look for something, the ParallelLoopState.Stop() is recommend.
and we could see that ParallelLoopState.<g class="gr_ gr_12894 gralert gr_gramm gr_inline_cards gr_run_anim Grammar onlyins doubleReplace replaceWithoutSep" id="12894" datagrid="12894">Stop</g> will stop until the iteration 97 <g class="gr_ gr_12895 gralert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="12895" datagrid="12895">completed;</g> but ParallelLoopState.Break() will continue which all maybe have opened <g class="gr_ gr_14158 gralert gr_gramm gr_inline_cards gr_run_anim Grammar onlyins doubleReplace replaceWithoutSep" id="14158" datagrid="14158">thread</g> <g class="gr_ gr_14101 gralert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="14101" datagrid="14101">,then</g> stop.
Completed iteration 11
Completed iteration 51
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 17
Completed iteration 18
Completed iteration 19
Completed iteration 38
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 43
Completed iteration 44
Completed iteration 45
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
Completed iteration 46
Completed iteration 47
Completed iteration 48
53
done
Completed iteration 73
Completed iteration 49
Stop in iteration 97
Completed iteration 2
Completed iteration 29
Completed iteration 97
10
done
Guide
With regards, Angelina Jolie
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 6, 2018 9:14 AM
All replies

User832373396 posted
Hi vinodkpasi,
Sir, here is the testing example
 ParallelLoopState.Break()
[TestMethod] public void TestMethod16() { var result = Parallel.For(1, 10, (i, state) => { if (i > 3) { Debug.WriteLine("Break in iteration {0}", i); state.Break(); } Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine("done"); }
Output
Completed iteration 1 Completed iteration 3 Break in iteration 5 Completed iteration 5 Completed iteration 2 Break in iteration 7 Completed iteration 7 Break in iteration 9 Break in iteration 4 Completed iteration 4 Completed iteration 9 done
 ParallelLoopState.Stop()
[TestMethod] public void TestMethod17() { var result = Parallel.For(1,10, (i, state) => { if (i > 3) { Debug.WriteLine("Stop in iteration {0}", i); state.Stop(); return; } Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine("done" ); }
Output
Completed iteration 1 Completed iteration 3 Stop in iteration 4 Stop in iteration 7 Stop in iteration 9 Stop in iteration 5 Completed iteration 2 done
It seems that it is a bit different between them.
Next
Test example
1 ParallelLoopState.Break() 2 ParallelLoopState.Stop() [TestMethod] public void TestMethod16() { var c = 0; var result = Parallel.For(1, 100, (i, state) => { //if (state.ShouldExitCurrentIteration) //{ // if (state.LowestBreakIteration < i) // return; //} if (i>50) { Debug.WriteLine("Break in iteration {0}", i); state.Break(); } c++; Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine(c); Debug.WriteLine("done"); }
[TestMethod] public void TestMethod17() {
var c = 0; var result = Parallel.For(1,100, (i, state) => {
if (i > 50) { Debug.WriteLine("Stop in iteration {0}", i); state.Stop();
//return ; } //if (state.IsStopped) //{ // return; //} c++; Debug.WriteLine("Completed iteration {0}", i); }); Debug.WriteLine(c); Debug.WriteLine("done" ); } }
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Completed iteration 29
Completed iteration 30
Completed iteration 2
Completed iteration 49
Break in iteration 97
Break in iteration 73
Completed iteration 73
Completed iteration 97
Completed iteration 31
Completed iteration 50
Break in iteration 51
Completed iteration 51
Completed iteration 3
Completed iteration 4
Completed iteration 5
Completed iteration 6
Completed iteration 7
Completed iteration 8
Completed iteration 9
Completed iteration 10
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 38
Completed iteration 11
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 17
Completed iteration 18
Completed iteration 43
Completed iteration 19
Completed iteration 44
Completed iteration 45
Completed iteration 46
Completed iteration 47
Completed iteration 48
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
53
Done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Completed iteration 29
Stop in iteration 73
Completed iteration 49
Completed iteration 2
Completed iteration 73
Stop in iteration 97
Completed iteration 97
Completed iteration 30
11
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Break in iteration 73
Completed iteration 73
Completed iteration 29
Completed iteration 49
Completed iteration 2
Completed iteration 3
Break in iteration 97
Completed iteration 97
Completed iteration 4
Completed iteration 5
Completed iteration 50
Break in iteration 51
Completed iteration 30
Completed iteration 31
Completed iteration 6
Completed iteration 7
Completed iteration 8
Completed iteration 9
Completed iteration 10
Completed iteration 11
Completed iteration 51
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 17
Completed iteration 18
Completed iteration 19
Completed iteration 38
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 43
Completed iteration 44
Completed iteration 45
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
Completed iteration 46
Completed iteration 47
Completed iteration 48
53
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Completed iteration 29
Stop in iteration 73
Completed iteration 73
Stop in iteration 97
Completed iteration 2
Completed iteration 49
Completed iteration 30
Completed iteration 97
11
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Break in iteration 73
Completed iteration 73
Completed iteration 29
Completed iteration 49
Completed iteration 2
Completed iteration 3
Break in iteration 97
Completed iteration 97
Completed iteration 4
Completed iteration 5
Completed iteration 50
Break in iteration 51
Completed iteration 30
Completed iteration 31
Completed iteration 6
Completed iteration 7
Completed iteration 8
Completed iteration 9
Completed iteration 10
Completed iteration 11
Completed iteration 51
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 17
Completed iteration 18
Completed iteration 19
Completed iteration 38
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 43
Completed iteration 44
Completed iteration 45
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
Completed iteration 46
Completed iteration 47
Completed iteration 48
53
done
Completed iteration 1
Completed iteration 25
Completed iteration 26
Completed iteration 27
Completed iteration 28
Stop in iteration 73
Completed iteration 73
Completed iteration 49
Stop in iteration 97
Completed iteration 2
Completed iteration 29
Completed iteration 97
10
done
Elapsed time
Elapsed time
about 303ms
about 94ms
We could see that if I remove the specified code for them, they are almost same. but from the test result, we could view the difference that if we use stop, it will stop reusing more resources quickly. and as the official document tells, if we want to look for something, the ParallelLoopState.Stop() is recommend.
and we could see that ParallelLoopState.<g class="gr_ gr_12894 gralert gr_gramm gr_inline_cards gr_run_anim Grammar onlyins doubleReplace replaceWithoutSep" id="12894" datagrid="12894">Stop</g> will stop until the iteration 97 <g class="gr_ gr_12895 gralert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="12895" datagrid="12895">completed;</g> but ParallelLoopState.Break() will continue which all maybe have opened <g class="gr_ gr_14158 gralert gr_gramm gr_inline_cards gr_run_anim Grammar onlyins doubleReplace replaceWithoutSep" id="14158" datagrid="14158">thread</g> <g class="gr_ gr_14101 gralert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="14101" datagrid="14101">,then</g> stop.
Completed iteration 11
Completed iteration 51
Completed iteration 32
Completed iteration 33
Completed iteration 34
Completed iteration 35
Completed iteration 36
Completed iteration 37
Completed iteration 12
Completed iteration 13
Completed iteration 14
Completed iteration 15
Completed iteration 16
Completed iteration 17
Completed iteration 18
Completed iteration 19
Completed iteration 38
Completed iteration 39
Completed iteration 40
Completed iteration 41
Completed iteration 42
Completed iteration 43
Completed iteration 44
Completed iteration 45
Completed iteration 20
Completed iteration 21
Completed iteration 22
Completed iteration 23
Completed iteration 24
Completed iteration 46
Completed iteration 47
Completed iteration 48
53
done
Completed iteration 73
Completed iteration 49
Stop in iteration 97
Completed iteration 2
Completed iteration 29
Completed iteration 97
10
done
Guide
With regards, Angelina Jolie
 Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, February 6, 2018 9:14 AM 
User1904516115 posted
Hi Angelina,
Thanks for reply. My doubt got cleared with your example.
Tuesday, February 13, 2018 6:50 PM