static void Main() { var BreakSource = Enumerable.Range(0, 1000).ToList(); int BreakData = 0; Console.WriteLine("Using loopstate Break Method"); Parallel.For(0, BreakSource.Count, (i, BreakLoopState) => { BreakData += i; if (BreakData > 100) { BreakLoopState.Break(); Console.WriteLine("Break called iteration {0}. data = {1} ", i, BreakData); } }); Console.WriteLine("Break called data = {0} ", BreakData); var StopSource = Enumerable.Range(0, 1000).ToList(); int StopData = 0; Console.WriteLine("Using loopstate Stop Method"); Parallel.For(0, StopSource.Count, (i, StopLoopState) => { StopData += i; if (StopData > 100) { StopLoopState.Stop(); Console.WriteLine("Stop called iteration {0}. data = {1} ", i, StopData); } }); Console.WriteLine("Stop called data = {0} ", StopData); Console.ReadKey(); }
static void Main(string[] args) { DateTime StartDateTime = DateTime.Now; Console.WriteLine(@"Parallel For Loop Execution start at : {0}", StartDateTime); Parallel.For(0, 10, i => { long total = DoSomeIndependentTask(); Console.WriteLine("{0} - {1}", i, total); }); DateTime EndDateTime = DateTime.Now; Console.WriteLine(@"Parallel For Loop Execution end at : {0}", EndDateTime); TimeSpan span = EndDateTime - StartDateTime; int ms = (int)span.TotalMilliseconds; Console.WriteLine(@"Time Taken to Execute the Loop in miliseconds {0}", ms); // Using the Degree of parallelism we can specify the maximum number of threads to be used to execute the program. //Limiting the maximum degree of parallelism to 2 var options = new ParallelOptions() { MaxDegreeOfParallelism = 2 }; int n = 10; Parallel.For(0, n, options, i => { Console.WriteLine(@"value of i = {0}, thread = {1}", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(10); }); // use BreakLoopState to Break the Method BreakLoopState.Break(); var BreakSource = Enumerable.Range(0, 1000).ToList(); int BreakData = 0; Console.WriteLine("Using loopstate Break Method"); Parallel.For(0, BreakSource.Count, (i, BreakLoopState) => { BreakData += i; if (BreakData > 100) { BreakLoopState.Break(); Console.WriteLine("Break called iteration {0}. data = {1} ", i, BreakData); } }); Console.WriteLine("Break called data = {0} ", BreakData); Console.WriteLine("Press any key to exist"); Console.ReadLine(); }
public static void RunBreakingOutOfParallelForLoop() { var BreakSource = Enumerable.Range(0, 1000).ToList(); int BreakData = 0; Console.WriteLine("Using loopstate Break Method"); Parallel.For(0, BreakSource.Count, (i, BreakLoopState) => { BreakData += i; if (BreakData > 100) { BreakLoopState.Break(); Console.WriteLine("Break called iteration {0}. data = {1} ", i, BreakData); } }); Console.WriteLine("Break called data = {0} ", BreakData); var StopSource = Enumerable.Range(0, 1000).ToList(); int StopData = 0; Console.WriteLine("Using loopstate Stop Method"); Parallel.For(0, StopSource.Count, (i, StopLoopState) => { StopData += i; if (StopData > 100) { StopLoopState.Stop(); Console.WriteLine("Stop called iteration {0}. data = {1} ", i, StopData); } }); Console.WriteLine("Stop called data = {0} ", StopData); Console.ReadKey(); //In a Parallel.For or Parallel.ForEach loop, you cannot use the same break or Exit statement that is used in a sequential loop because those language constructs are valid for loops, and a parallel “loop” is actually a method, not a loop. Instead, you use either the Stop or Break method. //Iterator will not instantly stop when reaches 100 }