I have an application similar to sample C# code this link.
In this example, there is no limit to the no of threads generated.
Could someone provide me sample code to limit the no of thread (read from config file) generated.
I am using framework 3.5. i don’t have a provision to upgrade to 4.0.
Thanks in advance –Joxin
The
ThreadPoolclass does have the option toSetMaxThreads, which will allow you to set the maximum number of threads.That being said, this is rarely a good idea – at least not as a permanent setting/change. It can be beneficial to do this, and then restore the previous max threads, for a specific algorithm, however.
Note that .NET 4’s TPL does make this far simpler, and is usable in .NET 3.5 if you install the Rx framework, as the 3.5 version includes a back port of the TPL for .NET 3.5. This provides the functionality to limit the work directly for a routine (via ParallelOptions.MaxDegreeOfParallelism).