"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
"tshad" <ts***@dslextre me.comwrote in message
news:eU******** ******@TK2MSFTN GP03.phx.gbl...
>>I need to find out how many threads a process has.
I have used ThreadPool.GetA vailableThreads and ThreadPool.GetM axThreads,
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.Diagnost ics.Process.Thr eads returns a collection of thread in the
process;
using System.Diagnost ics;
...
ProcessThreadCo llection myThreads = GetCurrentProce ss().Threads;
That worked after I changed it to Process.GetCurr entProcess().Th reads.
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.Diagnost ics.ProcessThre adCollection
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(objec t source, FileSystemEvent Args e)
{
fileWatchThread = new Thread(WatchThe Files);
threadList.Add( fileWatchThread );
fileWatchThread .Name = "fileWatchThrea d";
fileWatchThread .Start();
}
private void WatchTheFiles()
{
FileStream fs = new FileStream(@"c: \tomsfilewatche r\tomsfilewatch er.txt",
FileMode.Append , System.IO.FileA ccess.Write);
StreamWriter sw = new StreamWriter(fs );
sw.WriteLine("S omething Changed");
sw.Close();
System.Threadin g.Thread.Sleep( 60000);
// File.Move(@"c:\ tomstest\tomste st.txt",
@"c:\tomstest2\ tomstest.txt");
}
*************** *************** *************** ***************
Thanks,
Tom
...
Willy.