multithread problem

sdlangers

Centurion
Joined
Dec 3, 2002
Messages
118
hi,

i have a simple instant message example app. for now, it just replies with whatever message you enter. it works perfectly, except when I close the form, the thread does not exit and the process continues to run.

please help
thanks

here is the form1 code:

Visual Basic:
    Private WithEvents l As Listener
    Private thdListener As Thread
    Private objClient As TcpClient
    Private strFriend As String
    Private strMe As String

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        rtbMessage.Text += strMe + ": " + rtbType.Text + vbCrLf

        objClient = New TcpClient("127.0.0.1", 1000)

        Dim w As StreamWriter = New StreamWriter(objClient.GetStream())
        w.Write(rtbType.Text + vbCrLf)
        w.Flush()

        objClient.Close()

        rtbType.Text = ""
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strFriend = "Friend"
        strMe = "Me"
        l = New Listener
        thdListener = New Thread(AddressOf l.Listen)
        thdListener.Start()
    End Sub

    Private Sub l_messageReceived(ByVal strMessage As String) Handles l.messageReceived
        rtbMessage.Text += strFriend + ": " + strMessage + vbCrLf
    End Sub

    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        thdListener.Abort()        
    End Sub

and here is the listener class
Visual Basic:
Public Class Listener
    Public Event messageReceived(ByVal strMessage As String)

    Private objListener As TcpListener
    Private objClient As TcpClient


    Public Sub Listen()
        Dim strTemp As String = ""
        Dim ip As Net.IPAddress
        ip.Parse("127.0.0.1")
        objListener = New TcpListener(ip, 1000)

        objListener.Start()
        Do
            objClient = objListener.AcceptTcpClient()
            Dim objReader As StreamReader = New StreamReader(objClient.GetStream())
            While (objReader.Peek() <> -1)
                strTemp += Convert.ToChar(objReader.Read()).ToString()
            End While
            RaiseEvent messageReceived(strTemp)
            strTemp = ""            
        Loop
        objListener.Stop()
    End Sub

    
End Class
 
I've heard that sometimes the thread.abort thing doesn't work. Some fixes I've heard are to simply call thread.abort something like 30 times. Or possibly on closing set a boolean called closed to true, and have your thread loop check for that boolean and if it's true, exit the loop.
 
If you make the thread a background thread it should terminate automatically when the application is closed e.g.

Visual Basic:
thdListener = New Thread(AddressOf l.Listen)
thdListener.Start()
'Make the thread a background thread
'This might not be the exact syntax as i dont have VS open at the moment
'just take a look through the intellisense properties if it isnt correct
thdListener.IsBackground = True
 
Another way would be to wait untill the abort finished. This can take some time but there is a simple way to wait for it.
After calling Abort() on the thdListener, call Join() on it as well. That lets your main thread wait until the thread has aborted.
 
Back
Top