Click here to Skip to main content
15,890,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,
I am developing one application in VB.net for Serialport reading from GSM Modem......While i read data from serialport that is append to string variable, after that i need to check weather that text is valid or not , if data is valid i append that text to TextBox.Text here I get one runtime error ie.,"Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on." ------- InvalidOperation exception was unhandled....

Here I provide MY TOTAL CODE


Imports System.IO.Ports
Imports System.Windows.Forms.Form
Imports System
Public Class Main
    Dim WithEvents serialport As New SerialPort()
    Dim Rxd As String
    Public Rxd1 As String
    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each sp As String In My.Computer.Ports.SerialPortNames
            ComboBox1.Items.Add(sp)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            With serialport
                .PortName = ComboBox1.Text
                .BaudRate = "9600"
                .Parity = Parity.None
                .StopBits = StopBits.One
                .DataBits = 8
                .Handshake = Handshake.None
                .ReadBufferSize = 500
                .Open()
                AddHandler serialport.DataReceived, AddressOf serialport_DataReceived
                '.ReadBufferSize = 500
            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Height = 743
        Me.Width = 1029
        Label8.Height = 30
        Label8.Width = 85
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If serialport.IsOpen = True Then
            serialport.Write("$M1#")
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        serialport.WriteLine("$M1#")
    End Sub

    Private Sub serialport_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
        Dim sp As SerialPort = CType(sender, SerialPort)

        Dim menu_count As Integer
        Dim res As Integer
        Rxd = sp.ReadExisting
        menu_count = InStr(1, Rxd, "$M2", vbTextCompare)
        If menu_count > (500 - 189) Then
            sp.Write("$M1#")
        ElseIf menu_count Then
            menu_count = menu_count + 3
            For res = 0 To 5
                Rxd1 = Mid(Rxd, menu_count, 3)
                menu_count = menu_count + 3
                MsgBox(Rxd1)
                If res = 0 Then
                    Me.TextBox1.Text = Rxd1
                ElseIf res = 1 Then
                    TextBox2.Text = Rxd1
                ElseIf res = 2 Then
                    TextBox3.Text = Rxd1
                ElseIf res = 3 Then
                    TextBox4.Text = Rxd1
                ElseIf res = 4 Then
                    TextBox5.Text = Rxd1
                ElseIf res = 5 Then
                    TextBox6.Text = Rxd1
                End If
            Next
        End If
    End Sub


Me.TextBox1.Text = Rxd1
AT THIS POINT I GET ERROR

ANYBODY PLZZ HELP MEEEEE........
Posted

1 solution

You are trying to access the textbox on a thread (the socket return method) which is not the main UI thread. Hence you get the error.
You can solve this by using the invoke method as explained here[^].
 
Share this answer
 
v2
Comments
Himachandra 26-Dec-11 6:42am    
Thank U For u r answer..... is there any alternative for invoke .......Because i don't have much skills on invoke,Threads n UI n so......
Plzz Give me any suggestion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900