Hi All,

I'm trying to write my first serial communication app in VB.net 2005 and I keep getting this error message:

Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on.

I know it has something to do with delegates but I have no idea how to use it in my code. Your help would be very much appreciated



Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text Is "Open Port" Then

            SerialPort1.Open()

            Button1.Text = "Close Port"

            Button2.Enabled = True



        ElseIf Button1.Text Is "Close Port" Then

            SerialPort1.Close()

            Button1.Text = "Open Port"

            Button2.Enabled = False

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SerialPort1.WriteLine(TextBox1.Text)
        TextBox1.Text = ""

        ListBox1.Items.Add(TextBox1.Text)


    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        ListBox1.Items.Add(SerialPort1.ReadLine())        <--------------This is where I get the error message
    End Sub

End Class