Evaluating threads in an application

  • Thread starter Thread starter Jerry Spence1
  • Start date Start date
J

Jerry Spence1

How can I gain access to all the threads running in my application? I'm
looking for something like:

Application.Threads.count
Application.threads(item).IsAlive
Application.threads(item).Name

etc.

-Jerry
 
Jerry said:
How can I gain access to all the threads running in my application?

First get a System.Diagnostics.Process by calling
System.Diagnostics.Process.GetCurrentProcess.

Then look in the ProcessThreadCollection returned from Process.Threads
- this is a collection of ProcessThread objects.
I'm
looking for something like:

Application.Threads.count
System.Diagnostics.Process.GetCurrentProcess.Threads.Count

Application.threads(item).IsAlive

Interrogate the .ThreadState of each ProcessState. There's more to
thread state than alive or not alive...
Application.threads(item).Name

Threads don't have names, they have a .Id of type Integer.
 
Larry Lard said:
First get a System.Diagnostics.Process by calling
System.Diagnostics.Process.GetCurrentProcess.

Then look in the ProcessThreadCollection returned from Process.Threads
- this is a collection of ProcessThread objects.


Interrogate the .ThreadState of each ProcessState. There's more to
thread state than alive or not alive...


Threads don't have names, they have a .Id of type Integer.

Thanks Larry

Not being able to get at the thread names is a problem. I am trying to
monitor the state of the threads in my project. How can I identify each
thread? I have created about 6 threads (and know their names) but when I do
System.Diagnostics.Process.GetCurrentProcess.Threads.Count it returns about
23 so there is more going on than I am interested in.

-Jerry
 
Jerry said:
Thanks Larry

Not being able to get at the thread names is a problem. I am trying to
monitor the state of the threads in my project. How can I identify each
thread? I have created about 6 threads (and know their names) but when I do
System.Diagnostics.Process.GetCurrentProcess.Threads.Count it returns about
23 so there is more going on than I am interested in.

Well I must admit I have never done this kind of stuff, and it seems I
led you astray somewhat. It turns out there isn't necessarily aone to
one relationship between Thread objects (the things we create and
manipulate) and the actual OS level threads (as reported to us by
Diagnostics), so this is the wrong approach.

It looks like if you want to track your Thread objects properly, you
have to do it yourself, with an app-wide collection (with thread-safe
access, of course... possibly by wrapping it in something that mediates
access)
 
Back
Top