Threading - Closing Multiple Instances of Same Thread

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?
Ignyte_SoftwareAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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
käµfm³d 👽Commented:
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
Ignyte_SoftwareAuthor Commented:

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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

käµfm³d 👽Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
Line 12 should end in a closing paren, not and underscore  = )
0
Ignyte_SoftwareAuthor Commented:
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
käµfm³d 👽Commented:
Awesome. Glad it worked for you   = )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.