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:
and here is the listener class
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