Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ThreadPool Class
 SetMinThreads Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
<SecurityPermissionAttribute(SecurityAction.Demand, ControlThread := True)> _
Public Shared Function SetMinThreads ( _
    workerThreads As Integer, _
    completionPortThreads As Integer _
) As Boolean
Visual Basic (Usage)
Dim workerThreads As Integer
Dim completionPortThreads As Integer
Dim returnValue As Boolean

returnValue = ThreadPool.SetMinThreads(workerThreads, _
    completionPortThreads)
C#
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
public static bool SetMinThreads(
    int workerThreads,
    int completionPortThreads
)
Visual C++
[SecurityPermissionAttribute(SecurityAction::Demand, ControlThread = true)]
public:
static bool SetMinThreads(
    int workerThreads, 
    int completionPortThreads
)
JScript
public static function SetMinThreads(
    workerThreads : int, 
    completionPortThreads : int
) : boolean

Parameters

workerThreads
Type: System..::.Int32
The new minimum number of idle worker threads to be maintained by the thread pool.
completionPortThreads
Type: System..::.Int32
The new minimum number of idle asynchronous I/O threads to be maintained by the thread pool.

Return Value

Type: System..::.Boolean
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.

When all thread pool threads have been assigned to tasks, the thread pool does not immediately begin creating new idle threads. To avoid unnecessarily allocating stack space for threads, it creates new idle threads at intervals. The interval is currently half a second, although it could change in future versions of the .NET Framework.

If an application is subject to bursts of activity in which large numbers of thread pool tasks are queued, use the SetMinThreads method to increase the minimum number of idle threads. Otherwise, the built-in delay in creating new idle threads could cause a bottleneck. A small increase in the number of idle threads can produce a significant improvement in throughput.

NoteNote:

Unnecessarily increasing the number of idle threads can also cause performance problems. Stack space must be allocated for each thread. If too many tasks start at the same time, all of them might appear to be slow. Finding the right balance is a performance-tuning issue.

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.

Visual Basic
Imports System
Imports System.Threading

Public Class Test

    <MTAThread> _
    Public Shared Sub Main()
        Dim minWorker, minIOC As Integer
        ' 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) Then
            ' The minimum number of threads was set successfully.
        Else
            ' The minimum number of threads was not changed.
        End If
    End Sub
End Class
C#
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.
        }
    }
}
Visual C++
using namespace System;
using namespace System::Threading;
int main()
{
   int minWorker;
   int minIOC;

   // 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.
   }
}

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker