views:

72

answers:

2

Is there a way to find out how many managed thread I use (including ThreadPool)?

When I get count of unmanaged threads through GetProcess I have an insane number of it (21 at very beginning)

+1  A: 

That's not the way it works. Any thread in a managed program can execute managed code, including ones that were originally started as an unmanaged thread. Which most are, the main thread and any threadpool thread starts life executing purely unmanaged code. It thunks into managed code though the kind of gateway provided by Marshal.GetDelegateForFunctionPointer().

Seeing dozens of (otherwise inactive) threads is not unusual. They typically are threadpool threads and threads started by COM servers. .NET is missing the plumbing you'd need to use Thread.ManagedThreadId on those threads. That's intentional, a logical .NET thread doesn't have to be a physical operating system thread. Although there's no host in current use where that's not the case.

You will have to avoid having to ask the question.

Hans Passant
thanks for letting me know. however I'd like to have an answer. How to find out how many managed thread are created in my applications at any time?
Bobb
You create them, count them.
Hans Passant
thats the point! I use 3d party API and they dont want to disclose details, also it might use thread pool or another software etc....
Bobb
You've only progressed to stage #2 of the five stages of grief. In stage #3 you might consider explaining why counting threads is important to you.
Hans Passant
I needed it just to compare different versions and approaches. to minimize/optimize number of threads and have information talking to the API devs. So this question is not very important. CAS is important though. I am going to switch it of whether you like it or not! :-P
Bobb
+1  A: 

I have not checked whether it is possible using the debugging interfaces but since VS displays managed threads in its debugger, you should be able to get them in yours too.

In .NET, writing a debugger is much easier than you may expect. Implementing the debugger basically consists of implementing the ICorDebug interface.

There is a sample from Microsoft: Managed Debugger Sample

Marek