.NETGURU
Increasing number of threads in ThreadPool
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.sdk.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Paul Ireland
Does anyone know how to increase the number of threads in the ThreadPool?
The MSDN docs state:

"The thread pool has a default limit of 25 threads per available processor,
which could be changed using CorSetMaxThreads as defined in the mscoree.h
file."

Any ideas which component implements CorSetMaxThreads so I can call it?

Being able to set the max number of threads in the thread pool is important
because classes such as HttpWebRequest use thread pools.

Paul Ireland

Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
Sure you can, but you have to create a Interop assembly by hand (tlbimp is of no help here) based on the definitions in mscoree.h.

-------------------------------------
using System;
using System.Runtime.InteropServices;

/*
class DECLSPEC_UUID("CB2F6723-AB3A-11d2-9C40-00C04FA30A3E")

CorRuntimeHost;

*/

[
Guid("CB2F6723-AB3A-11D2-9C40-00C04FA30A3E"),
ComImport
]
class CorRuntimeHost
{
}
/*

MIDL_INTERFACE("84680D3A-B2C1-46e8-ACC2-DBC0A359159A")

ICorThreadpool : public IUnknown

*/

[
Guid("84680D3A-B2C1-46e8-ACC2-DBC0A359159A"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
interface ICorThreadPool
{
void slot0(); // v-table dummy slots, as you are only interested in the max threadpool settings
void slot1();
void slot2();
void slot3();
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 );
}

------------------------------

Compile this into a library and call it as:

.....

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

ICorThreadPool clrTp

clrTp = (ICorThreadPool)new CorRuntimeHost();

clrTp .CorGetMaxThreads(out maxWorkerThreads, out maxIOThreads);
clrTp .CorGetAvailableThreads(out availWorkerThreads, out availIOThreads);

clrTp .CorSetMaxThreads(...
....

Willy.

"Paul Ireland" <Click here to reveal e-mail address> wrote in message news:u9I8e#l1BHA.2212@tkmsftngp05...
[Original message clipped]

Reply to this message...
 
    
Paul Ireland
Willy,

Nice one - works a treat. Any idea why Microsoft didn't expose this
functionality in a more user friendly way?

Paul

"Willy Denoyette [MVP]" <Click here to reveal e-mail address> wrote in message
news:e9UP7lq1BHA.2080@tkmsftngp04...
> Sure you can, but you have to create a Interop assembly by hand (tlbimp is
of no help here) based on the definitions in mscoree.h.
[Original message clipped]

Reply to this message...
 
    
Tom Kaiser
Just to add a point here, increasing the number of threads in the
threadpool may only delay the problem. My first question would be why am I
running out of threadpool thread.

"Paul Ireland" <Click here to reveal e-mail address> wrote in message
news:OqNuKEi2BHA.1260@tkmsftngp04...
[Original message clipped]

Reply to this message...
 
    
Stephen Anders
Willy, Thanks for this code. I have compiled this and it performs correctly, but I have not been able to get it to work from VB.NET. I referenced the dll from my VB project and try to call it in a similar fashion as you described for C#. Is there something else that needs to be done for this to work in VB.NET?
Here is what I have:
...
Private Sub SetMaxThreads()
Try
Dim maxWorkerThreads As UInt32
Dim maxIOThreads As UInt32
Dim newWorkerThreads As UInt32
Dim newIOThreads As UInt32

Dim CLR_ThreadPool As ICorThreadPool
Dim runtimeHost As New CorRuntimeHost

CLR_ThreadPool = CType(runtimeHost, ICorThreadPool)

CLR_ThreadPool.CorGetMaxThreads(maxWorkerThreads, maxIOThreads) '<-- after this call, both are still 0

maxWorkerThreads = 15 'set them here
maxIOThreads = 10

CLR_ThreadPool.CorSetMaxThreads(maxWorkerThreads, maxIOThreads) '<-- this throws an exception that is not caught by the try-catch block and halts program execution

CLR_ThreadPool.CorGetMaxThreads(newWorkerThreads, newIOThreads)

Catch ex As Exception ' I never get here
LogMessage(ex.Message, EventLogEntryType.Error, "PollingService.SetMaxThreads")
End Try
End Sub

The Try-Catch block does not catch the exception. When I step through the code, when I execute the .CorSetMaxThreads, I get a runtime exception that halts program execution instead of going to the catch block. Very curious...
Am I missing something, or is this not possible from VB.NET?
Thanks
--------------------------------
From: Stephen Anders
Reply to this message...
 
    
Brad Wilson
> Does anyone know how to increase the number of threads in the ThreadPool?

If I remember a thread pool discussion from correctly (it was a while ago),
the .NET thread pooling stuff is tied to the thread pooling in Win32, which
has a fixed thread pool size. It's almost certain that if you can change the
size of the thread pool, that those Cor* functions are meant to be called by
the unmanaged host, not from within .NET.

Brad

--
Read my weblog at http://www.quality.nu/dotnetguy/

Reply to this message...
 
 
System.Diagnostics.EventLogEntryType
System.Net.HttpWebRequest
System.Runtime.InteropServices.ComInterfaceType
System.Threading.ThreadPool
System.UInt32




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification