Limits on ThreadPool.SetMinThreads and SetMaxThreads
By Tan Lee Published on Jan 14, 2025 403
Imagine you're working on an ASP.NET Core application where you manage a pool of worker threads to process tasks. You might want to configure the minimum and maximum number of threads to optimize how requests are handled.
For example:
ThreadPool.SetMinThreads(workerThreads: 4, completionPortThreads: 4); ThreadPool.SetMaxThreads(workerThreads: 70, completionPortThreads: 70);
This configuration sets the minimum number of worker threads to 4 and the maximum number of worker threads to 70, along with corresponding completion port threads.
Are There Limitations on the Values You Can Set?
Yes, there are several limitations to be aware of when setting the minimum and maximum thread pool sizes in .NET Core.
The thread pool creates and manages worker and I/O completion threads as needed, aiming to optimize throughput by balancing the number of tasks completed over time. It can scale the number of threads up to a specified minimum, but when demand is low, the thread pool may use fewer threads than the minimum.
If you set a negative number or a value exceeding the maximum allowed thread pool threads (obtained with GetMaxThreads
), SetMinThreads
will return false and not update the minimum values. The thread pool has upper limits (e.g., short.MaxValue
), and the specified values are capped to these limits, meaning the actual thread count may be lower than requested even if the method succeeds.
- How to Initialize TagHelpers in ASP.NET Core with Shared Data
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core
- The request matched multiple endpoints in ASP.NET Core