
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How can I limit Parallel.ForEach in C#?
Parallel Foreach
Parallel.ForEach loop in C# runs upon multiple threads and processing takes place in a parallel way. Parallel.ForEach loop is not a basic feature of C# and it is available from C# 4.0 and above To use Parallel.ForEach loop we need to import System.Threading.Tasks namespace in using directive.
Foreach
Foreach loop in C# runs upon a single thread and processing takes place sequentially one by one. Foreach loop is a basic feature of C# and it is available from C# 1.0. Its execution is slower than the Parallel.Foreach in most of the cases.
Example 1
static void Main(string[] args){ List<string> alphabets = new List<string>(); alphabets.Add("A"); alphabets.Add("B"); alphabets.Add("C"); alphabets.Add("D"); alphabets.Add("E"); alphabets.Add("F"); alphabets.Add("G"); alphabets.Add("H"); alphabets.Add("I"); alphabets.Add("J"); alphabets.Add("K"); alphabets.Add("L"); alphabets.Add("M"); alphabets.Add("N"); alphabets.Add("O"); alphabets.Add("P"); alphabets.Add("Q"); alphabets.Add("R"); alphabets.Add("S"); alphabets.Add("T"); alphabets.Add("U"); alphabets.Add("V"); alphabets.Add("W"); alphabets.Add("X"); alphabets.Add("Y"); alphabets.Add("Z"); Console.WriteLine("Printing list using foreach loop
"); var stopWatch = Stopwatch.StartNew(); foreach (string alphabet in alphabets){ Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet, Thread.CurrentThread.ManagedThreadId); } Console.WriteLine("foreach loop execution time = {0} seconds
", stopWatch.Elapsed.TotalSeconds); Console.WriteLine("Printing list using Parallel.ForEach"); stopWatch = Stopwatch.StartNew(); Parallel.ForEach(alphabets, alphabet => { Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet, Thread.CurrentThread.ManagedThreadId); } ); Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", stopWatch.Elapsed.TotalSeconds); Console.Read(); Console.ReadLine(); }
Output
Printing list using foreach loop
alphabet Name: A, Thread Id= 1 alphabet Name: B, Thread Id= 1 alphabet Name: C, Thread Id= 1 alphabet Name: D, Thread Id= 1 alphabet Name: E, Thread Id= 1 alphabet Name: F, Thread Id= 1 alphabet Name: G, Thread Id= 1 alphabet Name: H, Thread Id= 1 alphabet Name: I, Thread Id= 1 alphabet Name: J, Thread Id= 1 alphabet Name: K, Thread Id= 1 alphabet Name: L, Thread Id= 1 alphabet Name: M, Thread Id= 1 alphabet Name: N, Thread Id= 1 alphabet Name: O, Thread Id= 1 alphabet Name: P, Thread Id= 1 alphabet Name: Q, Thread Id= 1 alphabet Name: R, Thread Id= 1 alphabet Name: S, Thread Id= 1 alphabet Name: T, Thread Id= 1 alphabet Name: U, Thread Id= 1 alphabet Name: V, Thread Id= 1 alphabet Name: W, Thread Id= 1 alphabet Name: X, Thread Id= 1 alphabet Name: Y, Thread Id= 1 alphabet Name: Z, Thread Id= 1 foreach loop execution time = 0.0223421 seconds
Printing list using Parallel.ForEach
alphabet Name: A, Thread Id= 1 alphabet Name: G, Thread Id= 4 alphabet Name: H, Thread Id= 4 alphabet Name: I, Thread Id= 4 alphabet Name: J, Thread Id= 4 alphabet Name: K, Thread Id= 4 alphabet Name: L, Thread Id= 4 alphabet Name: N, Thread Id= 4 alphabet Name: O, Thread Id= 4 alphabet Name: P, Thread Id= 4 alphabet Name: Q, Thread Id= 4 alphabet Name: Y, Thread Id= 6 alphabet Name: Z, Thread Id= 6 alphabet Name: D, Thread Id= 6 alphabet Name: E, Thread Id= 6 alphabet Name: F, Thread Id= 6 alphabet Name: T, Thread Id= 6 alphabet Name: U, Thread Id= 6 alphabet Name: V, Thread Id= 6 alphabet Name: R, Thread Id= 4 alphabet Name: M, Thread Id= 5 alphabet Name: S, Thread Id= 7 alphabet Name: B, Thread Id= 1 alphabet Name: C, Thread Id= 1 alphabet Name: W, Thread Id= 6 alphabet Name: X, Thread Id= 6 Parallel.ForEach() execution time = 0.0559777 seconds
Example 2
Limit the Paralleleism in parallel.foreach
static class Program{ static void Main(string[] args){ List<string> alphabets = new List<string>(); alphabets.Add("A"); alphabets.Add("B"); alphabets.Add("C"); alphabets.Add("D"); alphabets.Add("E"); alphabets.Add("F"); alphabets.Add("G"); alphabets.Add("H"); alphabets.Add("I"); alphabets.Add("J"); alphabets.Add("K"); alphabets.Add("L"); alphabets.Add("M"); alphabets.Add("N"); alphabets.Add("O"); alphabets.Add("P"); alphabets.Add("Q"); alphabets.Add("R"); alphabets.Add("S"); alphabets.Add("T"); alphabets.Add("U"); alphabets.Add("V"); alphabets.Add("W"); alphabets.Add("X"); alphabets.Add("Y"); alphabets.Add("Z"); Parallel.ForEach( alphabets, new ParallelOptions { MaxDegreeOfParallelism = 2 }, alphabet => { Console.WriteLine("alphabet Name: {0}, Thread Id= {1}", alphabet, Thread.CurrentThread.ManagedThreadId); } ); } }
Output
alphabet Name: N, Thread Id= 4 alphabet Name: O, Thread Id= 4 alphabet Name: P, Thread Id= 4 alphabet Name: A, Thread Id= 1 alphabet Name: B, Thread Id= 1 alphabet Name: C, Thread Id= 1 alphabet Name: Q, Thread Id= 4 alphabet Name: R, Thread Id= 4 alphabet Name: S, Thread Id= 4 alphabet Name: T, Thread Id= 4 alphabet Name: U, Thread Id= 4 alphabet Name: V, Thread Id= 4 alphabet Name: W, Thread Id= 4 alphabet Name: X, Thread Id= 4 alphabet Name: Y, Thread Id= 4 alphabet Name: Z, Thread Id= 4 alphabet Name: H, Thread Id= 4 alphabet Name: I, Thread Id= 4 alphabet Name: J, Thread Id= 4 alphabet Name: K, Thread Id= 4 alphabet Name: D, Thread Id= 1 alphabet Name: L, Thread Id= 4 alphabet Name: E, Thread Id= 1 alphabet Name: F, Thread Id= 1 alphabet Name: G, Thread Id= 1 alphabet Name: M, Thread Id= 4
- Related Articles
- What is the difference between Foreach and Parallel.Foreach in C#?
- How can I convert string to double in C/C++?
- Can I Win in C++
- How can I profile C++ code running in Linux?
- How can I clear console using C++?
- How do I use InputFilter to limit characters in an EditText in Android Kotlin?
- How can we limit the number of characters inside a JTextField in Java?
- How can I create directory tree using C++ in Linux?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- How can I get a file's size in C++?
- How can I convert Python tuple to C array?
- How can I profile C++ code running on Linux?
- How can I get the list of files in a directory using C/C++?
- When can I use a forward declaration in C/C++?
- How can I get the list of files in a directory using C or C++?

Advertisements