Question application hangs if i close the serial port

shigami35

New member
Joined
Dec 23, 2013
Messages
1
Programming Experience
1-3
please help me ..
if i close the serial port , the application has hangs ..


script:
 Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
        RemoveHandler SerialPort1.DataReceived, AddressOf DoUpdate
        System.Threading.Thread.Sleep(1000)
        If SerialPort1.IsOpen Then SerialPort1.Close()        
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                                    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                    Handles SerialPort1.DataReceived
        If ComOpen Then
            Try
                byteEnd = SerialPort1.NewLine.ToCharArray
                bacabuffer = SerialPort1.ReadLine()
                Me.Invoke(New EventHandler(AddressOf DoUpdate))
            Catch ex As Exception
                MsgBox("read" & ex.Message)
            End Try
        End If
    End Sub

    Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox1.Text = bacabuffer
    End Sub
 
Last edited by a moderator:
You only use RemoveHandler if you used AddHandler in the first place. It is completely unrelated to a Handles clause. The Handles clause follows the variable while AddHandler follows the object. What that means is that, when you use AddHandler, the event handler is attached to that object until the object is destroyed or RemoveHandler is executed while a Handles clause will always handle the specified event of the object that is currently assigned to the specified variable. Assign a new object to that variable and the the event handler sticks with the variable, thus handling the event of the new object.

If an application hangs then either it calls a method that never returns or or enters an infinite loop. I see no loop so I assume the former. Can you identify the line on which the unresponsive method is called?
 
Back
Top