ThreadPool.SetMinThreads Method (Int32, Int32)


Sets the minimum number of threads the thread pool creates on demand, as new requests are made, before switching to an algorithm for managing thread creation and destruction.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
public static bool SetMinThreads(
	int workerThreads,
	int completionPortThreads


Type: System.Int32

Theminimum number of worker threads that the thread pool creates on demand.

Type: System.Int32

The minimum number of asynchronous I/O threads that the thread pool creates on demand.

Return Value

Type: System.Boolean

true if the change is successful; otherwise, false.

The thread pool provides new worker threads or I/O completion threads on demand until it reaches the minimum for each category. When the minimum is reached, the thread pool can create additional threads in that category or wait until some tasks complete. Beginning with the .NET Framework 4, the thread pool creates and destroys threads in order to optimize throughput, which is defined as the number of tasks that complete per unit of time. Too few threads might not make optimal use of available resources, whereas too many threads could increase resource contention.

When demand is low, the actual number of thread pool threads can fall below the minimum values.

If you specify a negative number or a number larger than the maximum number of active thread pool threads (obtained using GetMaxThreads), SetMinThreads returns false and does not change either of the minimum values.


By default, the minimum number of threads is set to the number of processors on a system. You can use the SetMinThreads method to increase the minimum number ofthreads. However, unnecessarily increasing these values can cause performance problems. If too many tasks start at the same time, all of them might appear to be slow. In most cases, the thread pool will perform better with its own algorithm for allocating threads. Reducing the minimum to less than the number of processors can also hurt performance.

The following example sets the minimum number of worker threads to four, and preserves the original value for the minimum number of asynchronous I/O completion threads.

using System;
using System.Threading;

public class Test
    public static void Main()
        int minWorker, minIOC;
        // Get the current settings.
        ThreadPool.GetMinThreads(out minWorker, out minIOC);
        // Change the minimum number of worker threads to four, but
        // keep the old setting for minimum asynchronous I/O 
        // completion threads.
        if (ThreadPool.SetMinThreads(4, minIOC))
            // The minimum number of threads was set successfully.
            // The minimum number of threads was not changed.

for the ability to control threads. Permission value: ControlThread

.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft