Home > Development > Parallel Loops and Exiting Early

Parallel Loops and Exiting Early

Quick demo on how to exit early from a parallel loop. You can use either break nor stop. Stop terminate loops as early as possible while break lets early scheduled loops complete till completion.

using System;
using System.Threading.Tasks;

namespace ParallelLoops
    class Program
        public static void Main()
            Program program = new Program();

            Console.WriteLine("Press any key to quit...");

        public void ExitingLoopsPrematurely()
            var result = Parallel.For(1, 100, (value, state) =>
                                         if (value % 20 == 0)

                                         Console.WriteLine("Value: {0}", value);


            Console.WriteLine("Completed {0}, Exited at Iteration: {1}", result.IsCompleted, result.LowestBreakIteration );

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: