?
Solved

Threading - Closing Multiple Instances of Same Thread

Posted on 2011-10-04
7
Medium Priority
?
169 Views
Last Modified: 2012-05-12
Trying to understand how threading works, and particularly how to abort threads.

I am opening multiple instances of a thread.  I then use thread.abort, but it only aborts the first instance of the thread.  Is there a way for me to know for sure that all instances of this thread have been closed?  See sample code below:

*************************

        'First instance of thread
        t = New Thread(AddressOf DoSomething)
        t.Start()

        'Second instance of thread
        t = New Thread(AddressOf DoSomething)
        t.Start()

        'Third instance of thread
        t = New Thread(AddressOf DoSomething)
        t.Start()

        t.Abort() 'This is only aborting the first instance.  The second and third instances are still running

*************************

How can I make sure that all instances of the thread (t) are closed/aborted?
0
Comment
Question by:Ignyte_Software
  • 5
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36912555
Your variable t is pointed to a new Thread instance when you  in line 6 (the line after the comment). This does not mean that your first thread is deleted or even finished. This just means that you lost your pointer to that first thread, and the only thing you can do is let the first Thread naturally expire. In order to Abort each thread, you need to maintain a reference to each Thread object. An easy way to do this in your example is to create another variable:

Dim  t1, t2, t3 As Thread

'First instance of thread
t1 = New Thread(AddressOf DoSomething)
t1.Start()

'Second instance of thread
t2 = New Thread(AddressOf DoSomething)
t2.Start()

'Third instance of thread
t3 = New Thread(AddressOf DoSomething)
t3.Start()

t1.Abort()
t2.Abort()
t3.Abort()

Open in new window


There are other ways to maintain references to these objects, but try to start with this example to see if you grasp what is going on  = )
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36912611
Your variable t is pointed to a new Thread instance when you  in line 6 (the line after the comment).
Hmm.... Grammar doesn't seem to be my forte today  : \

Your variable t is pointed to a new Thread instance when you assign to it again in line 6 (the line after the comment).
0
 

Author Comment

by:Ignyte_Software
ID: 36912632

This won't work for me because I don't know how many instances there might be.  I can't set up a reference in advance for each thread, since I don't know how many there will be.

A real-world example is that I have a socket server that listens for connections.  When a client connects, I start a separate thread to process messages just for that one connection.  I might have an exception and want to close all of the connections (separate threads).

For example:

                Dim client As New MessageProcessor()
                client.StartClient()

I would like to be able to loop through all of the connected clients (separate threads) and abort each of them.  The threads will not have naturally expired.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36912659
This won't work for me because I don't know how many instances there might be.
Ah young paduwan, this is where my statment, :There are other ways to maintain references to these objects," came into play. An alternative method:

Dim threadList As New System.Collections.Generic.List(Of Thread)()

'First instance of thread
threadList.Add(New Thread(AddressOf DoSomething))
threadList(threadList.Count - 1).Start()

'Second instance of thread
threadList.Add(New Thread(AddressOf DoSomething))
threadList(threadList.Count - 1).Start()

'Third instance of thread
threadList.Add(New Thread(AddressOf DoSomething)_
threadList(threadList.Count - 1).Start()

For Each th As Thread In threadList
    th.Abort()
Next

Open in new window


Now you can have an arbitrary number of Threads in your List.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36912666
Line 12 should end in a closing paren, not and underscore  = )
0
 

Author Comment

by:Ignyte_Software
ID: 36912711
Yes, yes, yes . . . thank you!  This is what I was looking for.  I knew there had to be a way to loop through each thread and then abort it.  This is exactly what I need.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36912725
Awesome. Glad it worked for you   = )
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question