I have developed a .net application using .net framework 4.0 where I have used parallel.for() loop for multi threading. I have used the following configuration:
- Windows 7
- IIS 7.0
- Framework v4.0.30319
I have used ParallelOptions() class and the “MaxDegreeOfParallelism” property of this class is set to 5. I have logged the “Thread.CurrentThread.ManagedThreadId” in a log file to check whether 5 threads were created at the runtime. I have tested this application in two different environments. When the application is running at the environment where it has been developed it’s creating 5 thread IDs. But while it’s running at the other environment it’s creating 6 or 7 threads IDs. The application is compiled at the 1st environment only. I have used the published version of the application to test at the 2nd environment.
Can anyone help me by informing why this is happening and how to resolve it?
Parallel.Foruses thread pool threads. TheMaxDegreeOfParallelismcontrols the maximum concurrent number of threads being used, not 5 particular threads – the thread pool is at liberty to process work on any of its threads, andParallel.Formakes sure only 5 are running at the same time.