MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How many threads does a process use

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp
Author Message
tshad



Joined: 13 Dec 2007
Posts: 3

PostPosted: Wed Jan 30, 2008 6:52 pm    Post subject: How many threads does a process use Reply with quote

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

Archived from group: microsoft>public>dotnet>languages>csharp
Back to top
View user's profile Send private message
Willy Denoyette [MVP]



Joined: 08 Aug 2007
Posts: 116

PostPosted: Thu Jan 31, 2008 4:33 am    Post subject: Re: How many threads does a process use Reply with quote

"tshad" wrote in message @TK2MSFTNGP03.phx.gbl...
>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.
Back to top
View user's profile Send private message
tshad



Joined: 13 Dec 2007
Posts: 3

PostPosted: Wed Jan 30, 2008 10:25 pm    Post subject: Re: How many threads does a process use Reply with quote

"Willy Denoyette [MVP]" wrote in message
news:%23yMQgA5YIHA.2000@TK2MSFTNGP05.phx.gbl...
> "tshad" wrote in message
> @TK2MSFTNGP03.phx.gbl...
>>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;

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

> ...
> Willy.
>
>
Back to top
View user's profile Send private message
Willy Denoyette [MVP]



Joined: 08 Aug 2007
Posts: 116

PostPosted: Fri Feb 01, 2008 4:12 am    Post subject: Re: How many threads does a process use Reply with quote

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.

"tshad" wrote in message @TK2MSFTNGP05.phx.gbl...
>
> "Willy Denoyette [MVP]" wrote in message
> news:%23yMQgA5YIHA.2000@TK2MSFTNGP05.phx.gbl...
>> "tshad" wrote in message
>> @TK2MSFTNGP03.phx.gbl...
>>>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;
>
> 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
>
>> ...
>> Willy.
>>
>>
>
>
Back to top
View user's profile Send private message
tshad



Joined: 13 Dec 2007
Posts: 3

PostPosted: Thu Jan 31, 2008 10:17 pm    Post subject: Re: How many threads does a process use Reply with quote

"Willy Denoyette [MVP]" wrote in message @TK2MSFTNGP02.phx.gbl...
> 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

>
> Willy.
>
> "tshad" wrote in message
> @TK2MSFTNGP05.phx.gbl...
>>
>> "Willy Denoyette [MVP]" wrote in message
>> news:%23yMQgA5YIHA.2000@TK2MSFTNGP05.phx.gbl...
>>> "tshad" wrote in message
>>> @TK2MSFTNGP03.phx.gbl...
>>>>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;
>>
>> 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
>>
>>> ...
>>> Willy.
>>>
>>>
>>
>>
>
>
Back to top
View user's profile Send private message
Willy Denoyette [MVP]



Joined: 08 Aug 2007
Posts: 116

PostPosted: Fri Feb 01, 2008 3:52 pm    Post subject: Re: How many threads does a process use Reply with quote

"tshad" wrote in message @TK2MSFTNGP02.phx.gbl...
> "Willy Denoyette [MVP]" wrote in message
> @TK2MSFTNGP02.phx.gbl...
>> 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,

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.
Back to top
View user's profile Send private message
tshad



Joined: 08 Aug 2007
Posts: 9

PostPosted: Wed Feb 06, 2008 6:54 am    Post subject: Re: How many threads does a process use Reply with quote

"Willy Denoyette [MVP]" wrote in message @TK2MSFTNGP06.phx.gbl...
> "tshad" wrote in message
> @TK2MSFTNGP02.phx.gbl...
>> "Willy Denoyette [MVP]" wrote in message
>> @TK2MSFTNGP02.phx.gbl...
>>> 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,
>
> 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
> Willy.
>
>
Back to top
View user's profile Send private message
kevin



Joined: 06 Feb 2008
Posts: 1

PostPosted: Thu Feb 07, 2008 12:55 am    Post subject: Re: How many threads does a process use Reply with quote

This is only limited by your machine's memory


Kevin
Plenty of Software: Free downloads center
http://www.plentyofsoft.com
PAD Submit: http://www.plentyofsoft.com/submitpad.php


"tshad" wrote in message @TK2MSFTNGP02.phx.gbl...
>
> "Willy Denoyette [MVP]" wrote in message
> @TK2MSFTNGP06.phx.gbl...
>> "tshad" wrote in message
>> @TK2MSFTNGP02.phx.gbl...
>>> "Willy Denoyette [MVP]" wrote in message
>>> @TK2MSFTNGP02.phx.gbl...
>>>> 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,
>>
>> 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
>> Willy.
>>
>>
>
>

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Process.<memory> fields vs Perfmon Process memory counters? There are a number of fields in the Process object that appear to be similar to counters under the Process object in Perfmon. Is there a mapping of the Process field names to the Perfmon counters? Are they really the same values but with di

Launching threads from loop Hi, I have a question. I have a DLL created in C that does some file conversion. I have gone through the code to ensure that it is thread-safe, i.e. removing globals from the library, etc. I have a wrapper class in C#. I have tested the DLL/wrapper by cre

message loops and threads Hi, When I start a new messageloop by using does it create a new thread and execute the loop on that thread? If it does, why does the flow halt on the main thread? Where can I find more information on how message loop works

Real-time threads and GC I'm writing an app that does real time data acquisition, and needs to respond to DMA-complete events that get signalled every 10mS Currently I have some native C++ code that works very nicely, with a dedicated 'daemon' thread that waits on

Dllhost threshold for # of threads Hi, I have a COM+ Server running on an 8CPU box with Win2K3. The app is developed with VS.Net 2005 (C#) and uses SQL Server 2005 as the database. While stress testing the app, I start 100 clients that are passing the transactions to the COM+ Server. With
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group