473,769 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How many threads does a process use

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
Jan 30 '08 #1
3 3900
"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;
....
Willy.
Jan 30 '08 #2

"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.


Jan 31 '08 #3
FileSystemWatch er 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" <ts***@dslextre me.comwrote in message
news:eE******** ******@TK2MSFTN GP05.phx.gbl...
>
"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>"tshad" <ts***@dslextre me.comwrote in message
news:eU******* *******@TK2MSFT NGP03.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.Diagnos tics.Process.Th reads returns a collection of thread in the
process;

using System.Diagnost ics;
...
ProcessThreadC ollection 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.



Jan 31 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3206
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
34
10809
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
8
12949
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found that the object will not be garbage collected while the thread is running. Fair enough, the documented explanation is that the GC compresses objects on the heap and needs to update references, the references will be invalid for a couple of moments...
10
1761
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that T2, T4 and T5 are already running. Thanks, Darian
167
8345
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an object should run in its own thread, it should implement this mix-in class. Does this sound like plausible design decision? I'm surprised that C++ doesn't have such functionality, say in its STL. This absence of a thread/object relationship in...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.