Export (0) Print
Expand All

ThreadPool.SetMinThreads Method

Sets the number of idle threads the thread pool maintains in anticipation of new requests.

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

public static bool SetMinThreads (
	int workerThreads,
	int completionPortThreads
)
public static boolean SetMinThreads (
	int workerThreads, 
	int completionPortThreads
)
public static function SetMinThreads (
	workerThreads : int, 
	completionPortThreads : int
) : boolean

Parameters

workerThreads

The new minimum number of idle worker threads to be maintained by the thread pool.

completionPortThreads

The new minimum number of idle asynchronous I/O threads to be maintained by the thread pool.

Return Value

true if the change is successful; otherwise, false.

Idle threads are maintained by the thread pool in order to reduce the time required to satisfy requests for thread pool threads. Separate minimums are maintained for worker threads and asynchronous I/O threads. Idle threads in excess of the minimums are terminated, to save system resources. Maintenance of the idle threads is a background task.

NoteNote

GetMinThreads and SetMinThreads retrieve and set the total number of idle threads maintained by the thread pool, regardless of the number of processors in the computer.

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.

Caution noteCaution

Reducing the number of idle threads to less than the number of processors can hurt performance. Maintaining a large number of idle threads consumes system resources. You might need to adjust the number to achieve the best overall performance. A small increase in the number of idle threads can produce a significant improvement in throughput, in applications that have periods of inactivity followed by bursts of heavy activity.

The following example sets the minimum number of idle worker threads to four, and preserves the original value for the minimum number of idle 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.
        }
        else
        {
            // The minimum number of threads was not changed.
        }
    }
}

import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Test
{
    public static void main(String[] args)
    {
        int minWorker = 0;
        int minIOC = 0;

        // Get the current settings.
        ThreadPool.GetMinThreads(minWorker, 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.
        }
        else {
            // The minimum number of threads was not changed.
        }
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions

ADD
Show:
© 2015 Microsoft