Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2005

Tip: Looking for answers? Try searching our database.

Enumerating threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucvdv - 31 Mar 2005 11:16 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.