Hi,
I am having trouble updating a control on the main form (form1). I am new to threading and delegates. From what I could learn from the web, I have come up with the code below that "should" be thread safe. However, in the UpdateDisplay routine, I can write the received data to the message box but the form control update appears to do nothing?

Any help much appreciated.

Imports System.IO.Ports

Public Class SerialConnection
Dim port As SerialPort
Private comBuffer As Byte()

Public Sub New()
Me.port = New SerialPort
AddHandler port.DataReceived, AddressOf SerialPort_DataReceived
With Me.port
.BaudRate = 115200
End With
End Sub

Public Function Open(ByVal cport As String) As Boolean
If Me.port.IsOpen Then Me.port.Close()
Try
Me.port.PortName = cport
Me.port.Open()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & "Select a port from the list")
Return False
End Try
End Function

Public Sub close()
If Not Me.port.IsOpen Then Me.port.Close()
End Sub

Public Sub Send(ByVal txdata As String)
If Me.port.IsOpen Then
If txdata <> "" Then
Me.port.Write(txdata)
Else
MessageBox.Show("No Data to send")
End If
Else
MessageBox.Show("Cannot send Message, Port not open")
End If
End Sub

Public Sub ChangeRFPort()
Dim cport As String
cport = Form1.lstAvailablePorts.SelectedItem
'If Me.port.Open(cport) Then My.Settings.RFport = Form1.lstAvailablePorts.SelectedItem
End Sub


Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)

Dim n As Integer = Me.port.BytesToRead
comBuffer = New Byte(n - 1) {}
Me.port.Read(comBuffer, 0, n)

UpdateDisplay() 'use delegate to enure call me.processpacket for this instance

End Sub

Private Sub UpdateDisplay()
Dim mystr As String = System.Text.ASCIIEncoding.ASCII.GetString(comBuffer)

If Form1.lstReceived.InvokeRequired Then
Form1.lstReceived.Invoke(New MethodInvoker(AddressOf UpdateDisplay))
Else
Form1.lstReceived.Text = mystr 'this line is executed but appears to do nothing
MessageBox.Show(mystr) ' this line shows received data ok
End If

End Sub

End Class