Is there a way to enumerate Thread objects in the current application?
You can enumerate ProcessThread objects, but you can't get a thread's Name
property through those.
This compiles, but it crashes with an InvalidCastException in the "For"
line at runtime (sometimes it lists one thread correctly before it
crashes):
For Each t As Thread In Process.GetCurrentProcess.Threads
Debug.WriteLine("Thr " & t.Name & " state " & t.ThreadState.ToString)
Next
This won't compile because a ProcessThread cannot be converted to Thread:
For Each pt As ProcessThread In Process.GetCurrentProcess.Threads
Try
Dim t As Thread = CType(pt, Thread)
Debug.WriteLine(...
I've got a VB app that starts several worker threads.
Sometimes one of them hangs at exit, but I can't figure out which one, so I
would like to include code as above in the main form's Closed event.
David Levine - 31 Mar 2005 12:07 GMT
It's crashing because a process thread is not the same as a managed thread.
When you start a new thread you should give it a unique name. If your
program hangs you can then fire up the debugger and see which threads are
still alive. You should also keep track of all the threads you start and
manage them yourself. I go through a cleanup process where I manually cause
all threads to finish when the application is exiting -this ensures that
data does not get lost.
> Is there a way to enumerate Thread objects in the current application?
>
[quoted text clipped - 20 lines]
> I
> would like to include code as above in the main form's Closed event.