in Technology by
What is ParallelLoopState in C#?

1 Answer

0 votes
by
Suppose I want to stop my work after digging at a 10 mts. area. If only one worker was doing this task then it would have been easy. But since many are working in these tasks and each worker is unaware of what the other is doing, I need someone that can interact with each worker and know what is the state of each person's work. In other words before stopping the Parallel Task, I need to maintain a state of the Parallel Tasks executing. This task is handled by the ParallelLoopState class.
 
ParallelLoopState: Enables iterations of Parallel loops to interact with other iterations.
 
There are 2 methods Break () and Stop () to end the task.
 
The differences between the Break and Stop methods are
  • Break ensures that all earlier iterations that started in the loop are executed before exiting the loop.
  • Stop makes no such guarantees; it says that this loop is done and should exit as soon as possible.
  1. Parallel.For(0, 15, (int index, ParallelLoopState loopState) =>  
  2. {  
  3.     if (index == 10)  
  4.     {  
  5.         loopState.Break();  
  6.         //loopState.Stop();  
  7.     }  
  8.     Console.WriteLine("Digging for " + index + "mts. area ");  
  9.     Thread.Sleep(1000);                   
  10. }); 
Output
 
Show Output
 
Using Stop (), as you can see only 11 threads are executed and the task came to a halt.
 
threads
 
Using Break, as you can see one extra loop was executed. This is because as the parallel threads were executing, so it completed the ongoing thread and then stopped.
 

Related questions

0 votes
    Why do we use Async and Await in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What do you mean by value types and reference types in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    How you can implement nullable types in C#? explain with the syntax of Nullable type....
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is a multicast delegate in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is Garbage Collection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is Reflection in C#.Net?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
0 votes
    What is the difference between method overloading and method overriding in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between dispose() and finalize() methods in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between abstract class and interface in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    Selenium doesn’t support the following programming language: 1. Python 2. C# 3. C 4. Java...
asked Jul 11, 2021 in Technology by JackTerrance
0 votes
    What are partial classes in C#?...
asked Jul 9, 2021 in Technology by JackTerrance
0 votes
0 votes
0 votes
    What is File Handling in C#.Net?...
asked Apr 1, 2021 in Technology by JackTerrance
...