ThreadPool.SetMaxThreads(Int32, Int32) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets the number of requests to the thread pool that can be active concurrently. All requests above that number remain queued until thread pool threads become available.
public:
static bool SetMaxThreads(int workerThreads, int completionPortThreads);
public static bool SetMaxThreads (int workerThreads, int completionPortThreads);
static member SetMaxThreads : int * int -> bool
Public Shared Function SetMaxThreads (workerThreads As Integer, completionPortThreads As Integer) As Boolean
- workerThreads
- Int32
The maximum number of worker threads in the thread pool.
- completionPortThreads
- Int32
The maximum number of asynchronous I/O threads in the thread pool.
true
if the change is successful; otherwise, false
.
This method is not supported when the Windows thread pool is configured to be used instead of the .NET thread pool. For more information, see the Windows thread pool config setting.
You cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the number of processors on the computer. To determine how many processors are present, retrieve the value of the Environment.ProcessorCount property. In addition, you cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the corresponding minimum number of worker threads or I/O completion threads. To determine the minimum thread pool size, call the GetMinThreads method.
If the common language runtime is hosted, for example by Internet Information Services (IIS) or SQL Server, the host can limit or prevent changes to the thread pool size.
Use caution when changing the maximum number of threads in the thread pool. While your code might benefit, the changes might have an adverse effect on code libraries you use.
Setting the thread pool size too large can cause performance problems. If too many threads are executing at the same time, the task switching overhead becomes a significant factor.
Note
The thread pool may have upper limits for the maximum thread counts (such as short.MaxValue
, depending on the implementation). The argument values are capped to the upper limit, so even when the method returns true
, the actual maximum thread counts may be lower than requested.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: