Execute Loops In Parallell With Parallel Foreach

For and Foreach

Normally when you want to iterate over a collection you use for, foreach, while or do loops. When using a foreach loop you iterate over all the elements in a collection, unless you specifically tell it to end prematurely; the collection can be anything from a simple list to a table in a dataset.

When using a for loop to iterate you need to specify three values ​​in the declaration part of the loop:

  1. The first is an integer variable that will be used as an index position while looping. You also have to give the index a starting value.
  2. The second is the limitation of the index; how many iterations the loop will do. The loop will iterate until this expression yields false .
  3. The third is the increment or decrement of the index counter.

Parallel Execution

In some situations it is not efficient to use regular loops such as for and foreach; There will be instances when you need to speed up the execution, the solution is to use parallel execution. You can use parallel execution to execute methods in parallel instead of sequentially; the same applies to for and foreach loops. Let’s say that you have a method that executes several loops synchronously that take a long time to finish; to solve this dilemma you can have the loops execute in parallel on separate threads instead. By calling one of the For or ForEach methods on the static Parallel class, passing the method an Action <T> , you can do just that.

The Task Parallel Library contains the Parallel class that holds a number of methods that can be used to execute tasks at the same time, such as letting loops execute in parallel.

Parallel Foreach

The ForEach method on the static Parallel class has many overloads that takes different parameter lists; the simplest one takes two parameters:

  • A collection you want to iterate over, of type IEnumerable <TSource> .
  • A delegate function that will be executed once per iteration, of type Action <TSource> .

Parallel For

In a parallel for loop the data type Int32 is used for the from and to parameters of the loop, and the index parameter is executed as an Action <Int32> once per iteration. The Action <T> is typically a pre-defined method that is called as a delegate; but you could just as well use an inline method if that is more appropriate for your code.

Example of parallel execution

In the following example we use the ForEach method on the static Parallel class to iterate over a list of students; we execute the method PrintStudent for each student, to print the first name of student.

Note that the first parameter in the loop is the list of students, and the second is a Lambda expression that goes to the method that will be executed for each of the students in the list. The Lambda expression is a delegate defined with PLINQ that point to the method that will be executed.

 

Source by Jonas Fagerberg

private void ParallelLoop_Click (object sender, RoutedEventArgs e)
{
   Parallel.ForEach(students, student => PrintStudent (student));
}

private void PrintStudent (Student student)
{
  Console.WriteLine(student.FirstName);
}

Leave a Comment