Hi,
I want to create a fixed arbitrary size ThreadPool in .NET - I understand the default size is 25 - but I wish to have a different size e.g. 5 or 10. Anyone?
Hi,
I want to create a fixed arbitrary size ThreadPool in .NET - I understand the default size is 25 - but I wish to have a different size e.g. 5 or 10. Anyone?
ThreadPool.SetMaxThreads(5,5) and then anything over five threads will get queued.
You can use ThreadPool.SetMinThreads and ThreadPool.SetMaxThreads to have some control over the number of threads in the thread pool.
That being said, I recommend being cautious in using this. It's easy to get yourself into trouble, as many operations in the BCL rely on threadpool threads being available.
You should be careful about changing the size of the thread pool. There is just one fixed system thread pool, used by all kinds of things. Making it too small could cause problems in areas you didn't even think you were using.
If you want to have a relatively small thread pool for one specific task, you should use a separate pool. There are various third party pools available - I have a rather old one as part of MiscUtil, but it should be good enough for simple use cases. I'm sure you can find more advanced ones if you look.
It's unfortunate that there isn't an instantiable ThreadPool
in the framework yet. I can't remember offhand whether Parallel Extensions will effectively provide one, but I don't think it will.