1

My question is is there any way that we can limit the number of threads when we use the Parallel.For loop.

Suppose my machine has I3 processor and capable running 4 processes at a time. so when i run the Parallel.For loop in my machine, there will be 4 threads running in the background.

Is there any way to limit the number of threads?

Veeresh
  • 81
  • 1
  • 10
  • http://stackoverflow.com/questions/5512312/is-it-possible-to-limit-the-cores-for-parallel-foreach http://stackoverflow.com/questions/1114317/does-parallel-foreach-limits-the-number-of-active-threads – MarcD Sep 26 '12 at 09:32

1 Answers1

1

You can use ParallelOptions with MaxDegreeOfParallelism = 4

 Parallel.ForEach(words, 
                  new ParallelOptions {
                        MaxDegreeOfParallelism = 4
                        }, 
                  s => DoAction(s));
cuongle
  • 74,024
  • 28
  • 151
  • 206