Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

   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

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

Posted
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.
Posted

If you make the thread a background thread it should terminate automatically when the application is closed e.g.

 

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

Visit: VBSourceSeek - The VB.NET sourcecode library

 

 

"A mere friend will agree with you, but a real friend will argue."
Posted
If you make the thread a background thread it should terminate automatically when the application is closed e.g.

 

perfect - that works

 

thanks for your help

Posted

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.

Nothing is as illusive as 'the last bug'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...