469,282 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Count of system-wide threads?

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!
Feb 11 '07 #1
6 6184
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!!

--
Thinathayalan Ganesan, MCAD
"Gunga Din" wrote:
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!
Feb 12 '07 #2
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
Feb 12 '07 #3
"Gunga Din" <nonewrote in message news:0c*********************@giganews.com...
>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.

Feb 12 '07 #4
"Cyber Sannyasi" <Cy***********@discussions.microsoft.comwrote in message
news:20**********************************@microsof t.com...
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.
..

Feb 12 '07 #5
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Op*************@TK2MSFTNGP04.phx.gbl...
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.

Feb 12 '07 #6

"Cyber Sannyasi" <Cy***********@discussions.microsoft.comwrote in message
news:20**********************************@microsof t.com...
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...
Feb 12 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Niklas E | last post: by
5 posts views Thread by Eric Johannsen | last post: by
6 posts views Thread by Tejpal Garhwal | last post: by
68 posts views Thread by Martin Joergensen | last post: by
4 posts views Thread by Peter W Johnson | last post: by
12 posts views Thread by KWSW | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.