Increase ThreadPool size above 25.

September 3, 2007

.NET ThreadPool has a default maximum number of threads in pool which is 25 as documented. There might be a need to increase it, but there is no proper documented API to achieve this goal. This might be a requirement for many of us.

Here’s what you can do:

The Mscoree.h has the definitions for MAX limit of thread pool, and there are APIs for getting/setting it.

What is Mscoree.dll?
Mscoree.dll is the .NET Runtime Execution Engine.

So how can you change the default size of ThreadPool?
Using the definitions in Mscoree.h we can define a COM interop dll, as mentioned below
using System;
using System.Runtime.InteropServices;
[Guid(“CB2F6723-AB3A-11D2-9C40-00C04FA30A3E”), ComImport]
class CorRuntimeHost {}

[Guid(“84680D3A-B2C1-46e8-ACC2-DBC0A359159A”),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ICorThreadPool
{
void slot0(); // max threadpool settings
void slot1();
void slot2();
void slot3();
void slot4();
void slot5();
void slot6();
void CorSetMaxThreads( uint MaxWorkerThreads, uint MaxIOCompletionThreads );
void CorGetMaxThreads( out uint MaxWorkerThreads, out uint MaxIOCompletionThreads );
void CorGetAvailableThreads( out uint AvailableWorkerThreads, out uint AvailableIOCompletionThreads );
}
// Ok you are done with the COM interop, now build this into a class library (e.g. ThreadPool.dll )

Then you must consume the dll into you application, and using the APIs of this managed dll you can now change the default ThreadPool size. Here how you go about doing it.

uint maxWorkerThreads;
uint availWorkerThreads;
uint maxIOThreads;
uint availIOThreads;

ICorThreadPool iThreadPool ;
iThreadPool = (ICorThreadPool)new CorRuntimeHost();
iThreadPool.CorGetMaxThreads(out maxWorkerThreads, out maxIOThreads);
iThreadPool.CorGetAvailableThreads(out availWorkerThreads, out availIOThreads);
System.Console.Writeline(“Available worker threads : ” + availWorkerThreads);
System.Console.Writeline(“Available IO threads : ” + availIOThreads);
iThreadPool.CorSetMaxThreads(30,availIOThreads); // changing worker threadpool size from 25 to 30

Hmm! so we are done 🙂

-Bugs!

Advertisements

3 Responses to “Increase ThreadPool size above 25.”

  1. jp Says:

    Hi,

    This is exactly the kind of solution I have been looking for, but I have not been able to get it working correctly. Instead of 25, the available threads shows a very high number ( 74967000 in once case, but different on the two different dev boxes I tried it on) and then the call to iThreadPool.CorSetMaxThreads(30,availIOThreads); throws a FatalExecutionError.

    The text of the error is:

    The runtime has encountered a fatal error. The address of the error was at 0x7a0890c0, on thread 0x81c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

    Any thoughts on why this is occuring?

    Again.. thanks for the insights.

    >jp

  2. Bugs! Says:

    Hi uncle snuffagus,

    Can you reply to me which version of .NET you are using?
    If you are on .NET 2.0 there is another good way of acheiving the same.

    System.Threading.ThreadPool.GetAvailableThreads(out workerThreads, out IOThreads);
    System.Threading.ThreadPool.SetMaxThreads(out workerThreads, out IOThreads);

    🙂 this is pretty managed way.

  3. jp Says:

    Thanks…

    Yes, I am on .NET 2.0. Thanks for the suggestions… works like a charm.

    >jp


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: