views:

230

answers:

3

I have a multithreaded windows service application, I want to know every moment how many threads(with thread id, thread name, corresponding process id) are running which are created by my application.

Thank in advance.

+3  A: 

With C#

using System;
using System.Diagnostics;
class Program
{
    static void Main(string[] args)
    {
        Process proc = Process.GetCurrentProcess();
        Console.WriteLine(proc.Threads.Count.ToString());
    }
}

If you want working threads for some other process(not current app) change GetCurrentProcess() with GetProcessById(wanted proc) or GetProcessByName(wanted proc)

If you want to get something specific from the threads

proc.Threads[index].[take a look at the properties];

EDIT: How can I get all threads name of the process?

using System;
using System.Diagnostics;
class Program 
{ 
    static void Main(string[] args) 
    {
        Process[] proc = Process.GetProcessesByName("youprocess");
        Console.WriteLine(proc[0].Threads.Count.ToString()); 
    } 
}

GetProcessesByName returns an array of processes(there may be several processes with the same names). If you are sure there is only one - proc[0] is what you want. If There are several you can access them with index - proc[0], proc[1], etc.... proc is null if there are not processes with "your name"

Svetlozar Angelov
A: 

How can I get all threads name of the process?

+1  A: 

A great utility for doing what you describe is Process Explorer:

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx