How can I convert a foreach to Parallel.ForEach?

17,719

You can do:

Parallel.ForEach(delegates, handler => 
{ 
//your stuff 
});

Consider the following example

List<string> list = new List<string>()
{
    "ABC",
    "DEF", 
    "EFG"
};

Parallel.ForEach(list, str =>
{
    Console.WriteLine(str);
});

You may also see: How to: Write a Simple Parallel.ForEach Loop

Share:
17,719
persianLife
Author by

persianLife

Updated on June 05, 2022

Comments

  • persianLife
    persianLife almost 2 years

    how to convert:

      foreach (  NotifyCollectionChangedEventHandler handler in delegates) {
                ...
      }
    

    To somthing like This

     Parallel.ForEach(    NotifyCollectionChangedEventHandler handler in delegates) {
      ... 
     }
    
  • Chris Schiffhauer
    Chris Schiffhauer about 10 years
    Won't the Bitmap object leak memory on each iteration?