How can I convert this foreach code to Parallel.ForEach? How can I convert this foreach code to Parallel.ForEach? multithreading multithreading

How can I convert this foreach code to Parallel.ForEach?


Foreach loop:

  • Iterations takes place sequentially, one by one
  • foreach loop is run from a single Thread.
  • foreach loop is defined in every framework of .NET
  • Execution of slow processes can be slower, as they're run serially
    • Process 2 can't start until 1 is done. Process 3 can't start until 2 & 1 are done...
  • Execution of quick processes can be faster, as there is no threading overhead

Parallel.ForEach:

  • Execution takes place in parallel way.
  • Parallel.ForEach uses multiple Threads.
  • Parallel.ForEach is defined in .Net 4.0 and above frameworks.
  • Execution of slow processes can be faster, as they can be run in parallel
    • Processes 1, 2, & 3 may run concurrently (see reused threads in example, below)
  • Execution of quick processes can be slower, because of additional threading overhead

The following example clearly demonstrates the difference between traditional foreach loop and

Parallel.ForEach() Example

using System;using System.Diagnostics;using System.Threading;using System.Threading.Tasks;namespace ParallelForEachExample{    class Program    {        static void Main()        {            string[] colors = {                                  "1. Red",                                  "2. Green",                                  "3. Blue",                                  "4. Yellow",                                  "5. White",                                  "6. Black",                                  "7. Violet",                                  "8. Brown",                                  "9. Orange",                                  "10. Pink"                              };            Console.WriteLine("Traditional foreach loop\n");            //start the stopwatch for "for" loop            var sw = Stopwatch.StartNew();            foreach (string color in colors)            {                Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);                Thread.Sleep(10);            }            Console.WriteLine("foreach loop execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);            Console.WriteLine("Using Parallel.ForEach");            //start the stopwatch for "Parallel.ForEach"             sw = Stopwatch.StartNew();            Parallel.ForEach(colors, color =>            {                Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);                Thread.Sleep(10);            }            );            Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", sw.Elapsed.TotalSeconds);            Console.Read();        }    }}

Output

Traditional foreach loop1. Red, Thread Id= 102. Green, Thread Id= 103. Blue, Thread Id= 104. Yellow, Thread Id= 105. White, Thread Id= 106. Black, Thread Id= 107. Violet, Thread Id= 108. Brown, Thread Id= 109. Orange, Thread Id= 1010. Pink, Thread Id= 10foreach loop execution time = 0.1054376 seconds

Using Parallel.ForEach example

1. Red, Thread Id= 103. Blue, Thread Id= 114. Yellow, Thread Id= 112. Green, Thread Id= 105. White, Thread Id= 127. Violet, Thread Id= 149. Orange, Thread Id= 136. Black, Thread Id= 118. Brown, Thread Id= 1010. Pink, Thread Id= 12Parallel.ForEach() execution time = 0.055976 seconds


string[] lines = File.ReadAllLines(txtProxyListPath.Text);List<string> list_lines = new List<string>(lines);Parallel.ForEach(list_lines, line =>{    //Your stuff});


string[] lines = File.ReadAllLines(txtProxyListPath.Text);// No need for the list// List<string> list_lines = new List<string>(lines); Parallel.ForEach(lines, line =>{    //My Stuff});

This will cause the lines to be parsed in parallel, within the loop. If you want a more detailed, less "reference oriented" introduction to the Parallel class, I wrote a series on the TPL which includes a section on Parallel.ForEach.