Microsoft KB Archive/827419

= Information about the new GetMinThreads method and the new SetMinThreads method of the ThreadPool class in the .NET Framework 1.1 =

Article ID: 827419

Article Last Modified on 11/12/2004

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1

-





INTRODUCTION
The Microsoft .NET Framework 1.1 adds two new methods that follow:
 * SetMinThreads
 * GetMinThreads

These two new methods are for the ThreadPool class. The SetMinThreads method is used to increase the number of idle Worker threads or the number of I/O completion threads that the ThreadPool class maintains. The GetMinThreads method is used to obtain the current settings.



MORE INFORMATION
By default, the minimum number of idle threads that are maintained for the ThreadPool class is as follows:
 * One thread for each computer processor
 * One thread for each Worker thread category
 * One thread for each I/O category

The way that threads are allocated may cause a bottleneck if a particular application suddenly requires a larger number of threads. To avoid this problem, use the SetMinThreads method to increase the number of idle Worker threads and the number of I/O completion threads that the ThreadPool class maintains. Use the GetMinThreads method to obtain the current minimum settings.

SetMinThreads method
  The code example that follows provides the definition of the SetMinThreads method. public static bool SetMinThreads(int workerThreads, int completionPortThreads) { return SetMinThreadsNative(workerThreads, completionPortThreads); }  You can add the SetMinThreads method to increase the number of idle Worker threads and to increase the number of I/O completion threads that are maintained by the ThreadPool class. The SetMinThreads method sets the total number of idle threads, regardless of the number of processors that the computer has. If you specify a negative number or a number that is larger than the maximum number of active ThreadPool class threads that you can obtain from the GetMaxThreads method of the ThreadPool class, the SetMinThreads method returns false and does not change either of the minimum values.

GetMinThreads method
  The code example that follows provides the definition of the GetMinThreads method. public static void GetMinThreads(out int workerThreads, out int completionPortThreads) { GetMinThreadsNative(out workerThreads, out completionPortThreads); }  You can add the GetMinThreads method to the ThreadPool class as part of the .NET 1.1 Framework to obtain the number of idle Worker threads and the number of I/O completion threads that are maintained by the ThreadPool class.</li> The GetMinThreads method obtains the total number of idle threads regardless of the number of processors that a computer has.</li></ul>

Code samples that use the SetMinThreads method and the GetMinThreads method
In the following code samples, the minimum number of idle Worker threads is set to 4. The original value for the minimum number of idle I/O completion threads is preserved.

Microsoft Visual Basic .NET Dim minWorker, minIOC As Integer ' Get the current settings. ThreadPool.GetMinThreads(minWorker, minIOC) ' Change the minimum number of Worker threads to 4 but keep ' the old setting for the minimum I/O completion threads. If ThreadPool.SetMinThreads(4, minIOC) Then ' The minimum number of threads is set successfully. Else ' The minimum number of threads is not changed. End If Microsoft Visual C# .NET int minWorker, minIOC; // Get the current settings. ThreadPool.GetMinThreads(out minWorker, out minIOC); // Change the minimum number of Worker threads to 4 but keep // the old setting for minimum I/O completion threads. if (ThreadPool.SetMinThreads(4, minIOC)) {           // The minimum number of threads is set successfully. }       else {           // The minimum number of threads is not changed. }

<div class="references_section">