Link to home
Start Free TrialLog in
Avatar of kavvis
kavvis

asked on

VB.NET 2010 Exception Comport read

Hello I read from comport whit followed code:

Public Class Form1
    Public Event DataReceived As SerialDataReceivedEventHandler

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        InitCOM1()
        InitCOM2()
        InitCOM3()

    End Sub


    ''' <summary>
    ''' Initering Comport 1
    ''' </summary>
    Public Sub InitCOM1()

        Dim COM1Comport As New SerialPort("COM1")
        COM1Comport.BaudRate = 9600
        COM1Comport.Parity = Parity.Even
        COM1Comport.StopBits = StopBits.One
        COM1Comport.DataBits = 8
        COM1Comport.Handshake = Handshake.None
        AddHandler COM1Comport.DataReceived, AddressOf DataReceivedHandler
        COM1Comport.Open()
        ' COM1Comport.Close()

    End Sub

    ''' <summary>
    ''' Initering Comport 2
    ''' </summary>
    Public Sub InitCOM2()

        Dim COM2Comport As New SerialPort("COM3")
        COM2Comport.BaudRate = 9600
        COM2Comport.Parity = Parity.Even
        COM2Comport.StopBits = StopBits.One
        COM2Comport.DataBits = 8
        COM2Comport.Handshake = Handshake.None
        AddHandler COM2Comport.DataReceived, AddressOf DataReceivedHandler
        COM2Comport.Open()
        ' COM2Comport.Close()

    End Sub

    ''' <summary>
    ''' Initering Comport 3
    ''' </summary>
    Public Sub InitCOM3()

        Dim COM3Comport As New SerialPort("COM4")
        COM3Comport.BaudRate = 9600
        COM3Comport.Parity = Parity.Even
        COM3Comport.StopBits = StopBits.One
        COM3Comport.DataBits = 8
        COM3Comport.Handshake = Handshake.None
        AddHandler COM3Comport.DataReceived, AddressOf DataReceivedHandler
        COM3Comport.Open()
        ' COM3Comport.Close()

    End Sub



    Public Sub DataReceivedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        Dim sp As SerialPort = CType(sender, SerialPort)
        Dim indata As String = sp.ReadExisting()


        If sp.PortName = "COM3" Then
            ' MsgBox("COM3 " + indata)

            TextBox1.Text = ("COM3 " + indata)
        End If
        If sp.PortName = "COM4" Then

            TextBox1.Text = ("COM4 " + indata)
        End If

    End Sub

End Class

Open in new window

when I try to send the indata to a textbox on the form I get this ERROR

System.InvalidOperationException was unhandled
  Message=Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.set_WindowText(String value)
       at System.Windows.Forms.TextBoxBase.set_WindowText(String value)
       at System.Windows.Forms.Control.set_Text(String value)
       at System.Windows.Forms.TextBoxBase.set_Text(String value)
       at System.Windows.Forms.TextBox.set_Text(String value)
       at nyRs232.Form1.DataReceivedHandler(Object sender, SerialDataReceivedEventArgs e) in C:\Users\Automation West\Documents\Visual Studio 2010\Projects\nyRs232ff\nyRs232ff\Form1.vb:line 86
       at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
       at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
       at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  InnerException:



However I can send it to a MsgBox(indata) thats works..

What do I do wrong??
Avatar of SStory
SStory
Flag of United States of America image

Any time you use another thread you must realize that the UI elements are on different threads.  You cannot set them like this. You need a delegate.
ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kavvis
kavvis

ASKER

Thank you..

I try to get my littel code to work whit this..

http://www.vbforums.com/showthread.php?498387-Accessing-Controls-from-Worker-Threads

but can´t get it to run...  can you give me som tips.. How can I easy get it to run whit my littel code?
Avatar of kavvis

ASKER

I think I got what I needed.. thank you..
I have used this sort of method before and it worked. What problem are you having?