how to get managed .net threads status

B

bygandhi

Hi -

I am writing a service which will check a process and its threads for
their state ( alive or dead ).

The process has 5 .net managed threads created using thread.start and
each have been assigned a name.

As in .net there is no way we can get the managed threads, I am
thinking of making a win32 call and check their status.

I need help with regards to which win32 api functions will help me to
get a list of threads ( not OS threads but the .net managed threads )
and to check their status ( thread.IsAlive ,something like that ).

How do i accomplish this. ?

If there is another way I can get the process and get its threads to
check their status,rather than going win32 it will be helpful.

thanks
BG
 
W

Willy Denoyette [MVP]

Hi -

I am writing a service which will check a process and its threads for
their state ( alive or dead ).

The process has 5 .net managed threads created using thread.start and
each have been assigned a name.

As in .net there is no way we can get the managed threads, I am
thinking of making a win32 call and check their status.

Sure there is a way to get the managed threads state...
ProcessThread.ThreadState Property


Willy.
 
G

Guest

Hi -

ProcessThread collection gives me all the threads associated with the
process ( meaning all the OS threads as well ).

I have created 5 threads in my service using Thread.Start and also given a
name Thread.Name = "x".

when I do

Process [] proc = Process.GetProcessByName("MyProcess");

foreach ( ProcessThread thread in proc[0].Threads )

I get 30 threads that are executing. The thread state doesn't have IsAlive
property which System.Threading.Thread has.

System.Diagnostics.ProcessThread and System.Threading.Thread are no way
related.

I hope you understand my question. I want to check whther my main threads
that I created for that process are alive or not. ( not the OS threads that
may or may not include my threads).

Also ProcessThread.Thread has Id property which System.Threading.Thread
doesn't. Pluse the Ids keep changing.


--
Bhavin Gandhi
Software Developer
Rockwell Automation
USA
 
W

Willy Denoyette [MVP]

Oh, but you asked about the Win32 api's to get the thread status, right?
Managed threads aren't known by the OS so there are no Win32 API's to get
the 'status' of a managed threads.
All managed threads are associated with an OS thread, but if you are only
interested in the IsAlive property of a few of your threads you will have to
store the reference to the thread object in an array (or an hashtable keyed
with the thread name for easy lookup).
From this array you can select a thread to query its IsAlive property.

Willy.
Just wonder who created the other 25 threads in your process.

BGandhi said:
Hi -

ProcessThread collection gives me all the threads associated with the
process ( meaning all the OS threads as well ).

I have created 5 threads in my service using Thread.Start and also given a
name Thread.Name = "x".

when I do

Process [] proc = Process.GetProcessByName("MyProcess");

foreach ( ProcessThread thread in proc[0].Threads )

I get 30 threads that are executing. The thread state doesn't have IsAlive
property which System.Threading.Thread has.

System.Diagnostics.ProcessThread and System.Threading.Thread are no way
related.

I hope you understand my question. I want to check whther my main threads
that I created for that process are alive or not. ( not the OS threads
that
may or may not include my threads).

Also ProcessThread.Thread has Id property which System.Threading.Thread
doesn't. Pluse the Ids keep changing.


--
Bhavin Gandhi
Software Developer
Rockwell Automation
USA


Willy Denoyette said:
Sure there is a way to get the managed threads state...
ProcessThread.ThreadState Property


Willy.
 

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