How many threads does a process use

T

tshad

I need to find out how many threads a process has.

I have used ThreadPool.GetAvailableThreads and ThreadPool.GetMaxThreads, but
they both show 50 for worker threads and 100 for port threads. This stays
the same as each thread is opened up. After 3 or 4 are open the numbers are
the same.

How can I get the number of threads if this doesn't work?

Thanks,

Tom
 
W

Willy Denoyette [MVP]

tshad said:
I need to find out how many threads a process has.

I have used ThreadPool.GetAvailableThreads and ThreadPool.GetMaxThreads,
but they both show 50 for worker threads and 100 for port threads. This
stays the same as each thread is opened up. After 3 or 4 are open the
numbers are the same.

How can I get the number of threads if this doesn't work?

Thanks,

Tom


System.Diagnostics.Process.Threads returns a collection of thread in the
process;

using System.Diagnostics;
....
ProcessThreadCollection myThreads = GetCurrentProcess().Threads;
....
Willy.
 
T

tshad

Willy Denoyette said:
System.Diagnostics.Process.Threads returns a collection of thread in the
process;

using System.Diagnostics;
...
ProcessThreadCollection myThreads = GetCurrentProcess().Threads;

That worked after I changed it to Process.GetCurrentProcess().Threads.

But I thought after the thread left its code it would disappear.

Here are the results of my text:

In loop i = 8 Time: 1/30/2008 5:17:36 PMNumber of Threads: 5 Max
Threads: 50/1000 Available Threads: 50/1000 Current Threads:
System.Diagnostics.ProcessThreadCollection
Status for Thread: ThreadMonitor is Running
Status for Thread: fileWatchThread is Stopped
Status for Thread: fileWatchThread is Stopped
Status for Thread: fileWatchThread is WaitSleepJoin
Status for Thread: fileWatchThread is WaitSleepJoin

AS you can see 2 are stopped and 2 are WaitSleepJoin.

the Number Threads from your method is showing 5 which is the amount of
threads that were spawned, but it never decrements.

Do I need to do something to force this?

In the following you can see the Thread being started when a file hits my
folder. The thread then prints a message and after 60 seconds just ends
(which is where the Stopped is seen in my above results). A few seconds
later the other 2 would also show as stopped. But even after an hour the
number of threads are showing as 5.

*******************************************************
private void OnChanged(object source, FileSystemEventArgs e)
{
fileWatchThread = new Thread(WatchTheFiles);
threadList.Add(fileWatchThread);
fileWatchThread.Name = "fileWatchThread";
fileWatchThread.Start();
}

private void WatchTheFiles()
{
FileStream fs = new FileStream(@"c:\tomsfilewatcher\tomsfilewatcher.txt",
FileMode.Append, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Something Changed");
sw.Close();
System.Threading.Thread.Sleep(60000);
// File.Move(@"c:\tomstest\tomstest.txt",
@"c:\tomstest2\tomstest.txt");
}
************************************************************

Thanks,

Tom
 
W

Willy Denoyette [MVP]

FileSystemWatcher uses threads from the Threadpool to call you back when an
event occurs, these threads do return to the pool when done.
I don't see why you are spawning another thread from within OnChanged, this
method is on a thread pulled from the pool, so you are wasting one thread
per callback.

Willy.
 
T

tshad

Willy Denoyette said:
FileSystemWatcher uses threads from the Threadpool to call you back when
an event occurs, these threads do return to the pool when done.
I don't see why you are spawning another thread from within OnChanged,
this method is on a thread pulled from the pool, so you are wasting one
thread per callback.

Part of this was my fault I was printing the wrong thing (which was an array
of pointers I was keeping) instead of:
Process.GetCurrentProcess().Threads.Count.

But as far as the thread on OnChanged creating its own thread, that doesn't
seem to be the case (unless it always uses the same thread.

When I was looking at ProcessExplorer and watching the threads get created
and destroyed - if I moved 5 files into the target folder, 5 threads were
created (not 10). Unless I am missing something (which wouldn't be the
first time).

I did notice that when I started the service and looked how many threads
were running (without doing anything), there were about 12 threads started.

Thanks,

Tom
 
W

Willy Denoyette [MVP]

tshad said:
Part of this was my fault I was printing the wrong thing (which was an
array of pointers I was keeping) instead of:
Process.GetCurrentProcess().Threads.Count.

But as far as the thread on OnChanged creating its own thread, that
doesn't seem to be the case (unless it always uses the same thread.

When I was looking at ProcessExplorer and watching the threads get created
and destroyed - if I moved 5 files into the target folder, 5 threads were
created (not 10). Unless I am missing something (which wouldn't be the
first time).

I did notice that when I started the service and looked how many threads
were running (without doing anything), there were about 12 threads
started.

Thanks,

I'm afraid you are wrong, the FSW event handlers are running on a thread
from the threadpool.
In your case "OnChanged" is running on a TP thread, in "OnChanged" you start
a new thread, after which "Onstart" returns and the TP thread returns to the
pool. That means that you won't see these two threads running at the same
time, but you are starting a new thread needlessly. So, my question is - why
don't you run your WatchTheFiles code in OnChanged?

Willy.
 
T

tshad

Willy Denoyette said:
I'm afraid you are wrong, the FSW event handlers are running on a thread
from the threadpool.
In your case "OnChanged" is running on a TP thread, in "OnChanged" you
start a new thread, after which "Onstart" returns and the TP thread
returns to the pool. That means that you won't see these two threads
running at the same time, but you are starting a new thread needlessly.
So, my question is - why don't you run your WatchTheFiles code in
OnChanged?

You're right.

OnChanged does seem to start its own thread.

My problem would be in the OnStop routine where I would need to make sure
that the OnChanged wasn't still running when I stopped the service.

How would I check this without starting a thread and then checking the
status of the thread. I don't have a thread handle to check in the
OnChanged thread - do I?

Thanks,

Tom
 

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

Top