[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.Stop will stop until the iteration 97 completed; but ParallelLoopState.Break() will
continue which all maybe have opened thread ,then stop.

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

60 Points

201 Posts

## ParallelLoopState.Break() and ParallelLoopState.Stop()

Feb 05, 2018 05:20 PM|vinodkpasi|LINK

What is the difference between ParallelLoopState.Break() and ParallelLoopState.Stop()? Please give an example

Contributor

5260 Points

2307 Posts

## Re: ParallelLoopState.Break() and ParallelLoopState.Stop()

Feb 06, 2018 09:14 AM|AngelinaJolie|LINK

Hi vinodkpasi,

Sir, here is the testing example

ParallelLoopState.Break()OutputParallelLoopState.Stop()OutputIt seems that it is a bit different between them.

NextTest example1ParallelLoopState.Break()2ParallelLoopState.Stop()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 timeElapsed timeabout 303msabout 94msWe 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.Stop will stop until the iteration 97 completed; butParallelLoopState.Break() willcontinue which all maybe have openedthread ,thenstop.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

Guidehttps://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.break(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallelloopstate.stop(v=vs.110).aspx

With regards, Angelina JoliePlease remember to click "Mark as Answer" the responses that resolved your issue.

If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

60 Points

201 Posts

## Re: ParallelLoopState.Break() and ParallelLoopState.Stop()

Feb 13, 2018 06:50 PM|vinodkpasi|LINK

Hi Angelina,

Thanks for reply. My doubt got cleared with your example.