Count of system-wide threads?

  • Thread starter Thread starter Gunga Din
  • Start date Start date
G

Gunga Din

I must be missing it somehow...

I need to count the number of threads running, not just those started by my
application. Rather like the Task Manager, I would like to get the total
number of threads that are active.

What would be the most reliable way to obtain this number?

Thanks!
 
Hi Gunga,
To do this, you need to first import System.Diagnostic namespace, get the
current processes running on your machine and retrieve the total threadcounts
of each process.
See me code below,

using System.Diagnostics;


Process[] processList = Process.GetProcesses();
int threadCount = 0;
foreach (Process proc in processList)
{
threadCount = threadCount + proc.Threads.Count;
}

The threadCount gives you the final count of total number of active threads
on your machine...
Hope this helps!!
 
Note also that in 2.0 onwards managed threads don't always have a 1:1
mapping to system (process) threads. I only mention this in case you
need to account for some unexpected results.

Marc
 
Gunga Din said:
I must be missing it somehow...

I need to count the number of threads running, not just those started by my application.
Rather like the Task Manager, I would like to get the total number of threads that are
active.

What would be the most reliable way to obtain this number?

Thanks!


Read the performance counters using System.Diagnostics the counter to read is
System/Threads.

Willy.
 
Cyber Sannyasi said:
Hi Gunga,
To do this, you need to first import System.Diagnostic namespace, get the
current processes running on your machine and retrieve the total threadcounts
of each process.
See me code below,

using System.Diagnostics;


Process[] processList = Process.GetProcesses();
int threadCount = 0;
foreach (Process proc in processList)
{
threadCount = threadCount + proc.Threads.Count;
}

The threadCount gives you the final count of total number of active threads
on your machine...
Hope this helps!!

No need to enumerate the running processes, just need to read the System/Threads performnace
counter.

Willy.
..
 
Marc Gravell said:
Note also that in 2.0 onwards managed threads don't always have a 1:1 mapping to system
(process) threads. I only mention this in case you need to account for some unexpected
results.

Marc
Actually this is only true when managed code hosted in SQL2005, an environment which doesn't
allow (by default) to call System.Diagnostics methods anyway. The option to map fibers to
managed threads has not been implemented in V2 of the CLR, and AFAIK there are no direct
plans to implement this in version next either.

Willy.
 
Cyber Sannyasi said:
Hi Gunga,
To do this, you need to first import System.Diagnostic namespace, get the
current processes running on your machine and retrieve the total
threadcounts
of each process.
See me code below,

using System.Diagnostics;


Process[] processList = Process.GetProcesses();
int threadCount = 0;
foreach (Process proc in processList)
{
threadCount = threadCount + proc.Threads.Count;
}

The threadCount gives you the final count of total number of active
threads
on your machine...
Hope this helps!!



--

Thank you very much!

Exactly what I needed...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top