Hi! I have a slight problem with multithreading.

There is many threads active in the application
and they are all kept in an arraylist.

so when I try to quit the program, it loops through the arraylist and exits all the threads....

but something else is making the program "unwilling" to shut down, leaving it invisible, still running...

So I was wondering if there is a way to get ALL active (stillAlive) threads in the application and terminate them?

Recommended Answers

All 11 Replies

Terminate the thread once it finishes its work, don't wait to loop on the threads and Kill them...

That's not really what I want to do, It's for a chat program with each client having it's own thread to work on, so when the server is to shut down the loop sends a message to each connected client that it is shutting down, after that the thread is aborted.

So I don't really know why it won't shut down since the close sub checks if they are still alive and if so it terminates the thread :(

Give me your code "that loops for threads and kills them"

The clienthandler is a class I created which contains the clients Name, Thread & socket (tcpclient)

Sub ShutDown()
        Try
'here is the loop for the clients threads
            For n As Integer = 0 To ClientArray.Count - 1
                Dim ch As clienthandler = CType(ClientArray(n), clienthandler)
                Broadcast(ch, "QUIT|")
                ch.Sock.Close()
                ch.CLThread.Abort()
            Next
            DoListen = False
            listener.Stop()
'and this is for a listen loop that checks for incoming connections
            If ListenThread IsNot Nothing AndAlso ListenThread.IsAlive Then
                ListenThread.Abort()
            End If
        Catch ex As Exception
            MsgBox("(166): " & ex.Message & vbCrLf & vbCrLf & ">> Om du inte kan tyda detta felmeddelande kontakta Datanizze <<")
        End Try
        Me.Close()
    End Sub

thank you for the link! It worked (environment.exit)

However, at first I put the code in the wrong places making it quit a liiiittle too soon :D all fixed now (I think) :P ... but you know programming, solving one problem -> 10 new problems appears MAGICALLY :)

PS
Thou shall receive a reputation-link-click from me now :twisted:

You're welcome :) I'm happy you solved it but I don't understand your note!!

I don't understand your note!!

hehe.. I added your answer to your reputation by clicking
"Add to RamyMahrous's Reputation" as a thank you for helping me.

(oops dual posts)

loool you didn't :)

ooo yes I did.. but it might not have worked ;) I'll try again then :cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.