kavvis
asked on
VB.NET 2010 Exception Comport read
Hello I read from comport whit followed code:
System.InvalidOperationExc eption 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.Form s
StackTrace:
at System.Windows.Forms.Contr ol.get_Han dle()
at System.Windows.Forms.Contr ol.set_Win dowText(St ring value)
at System.Windows.Forms.TextB oxBase.set _WindowTex t(String value)
at System.Windows.Forms.Contr ol.set_Tex t(String value)
at System.Windows.Forms.TextB oxBase.set _Text(Stri ng value)
at System.Windows.Forms.TextB ox.set_Tex t(String value)
at nyRs232.Form1.DataReceived Handler(Ob ject sender, SerialDataReceivedEventArg s e) in C:\Users\Automation West\Documents\Visual Studio 2010\Projects\nyRs232ff\ny Rs232ff\Fo rm1.vb:lin e 86
at System.IO.Ports.SerialPort .CatchRece ivedEvents (Object src, SerialDataReceivedEventArg s e)
at System.IO.Ports.SerialStre am.EventLo opRunner.C allReceive Events(Obj ect state)
at System.Threading.QueueUser WorkItemCa llback.Wai tCallback_ Context(Ob ject state)
at System.Threading.Execution Context.ru nTryCode(O bject userData)
at System.Runtime.CompilerSer vices.Runt imeHelpers .ExecuteCo deWithGuar anteedClea nup(TryCod e code, CleanupCode backoutCode, Object userData)
at System.Threading.Execution Context.Ru nInternal( ExecutionC ontext executionContext, ContextCallback callback, Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUser WorkItemCa llback.Sys tem.Thread ing.IThrea dPoolWorkI tem.Execut eWorkItem( )
at System.Threading.ThreadPoo lWorkQueue .Dispatch( )
at System.Threading._ThreadPo olWaitCall back.Perfo rmWaitCall back()
InnerException:
However I can send it to a MsgBox(indata) thats works..
What do I do wrong??
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
when I try to send the indata to a textbox on the form I get this ERRORSystem.InvalidOperationExc
Message=Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
Source=System.Windows.Form
StackTrace:
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.TextB
at System.Windows.Forms.Contr
at System.Windows.Forms.TextB
at System.Windows.Forms.TextB
at nyRs232.Form1.DataReceived
at System.IO.Ports.SerialPort
at System.IO.Ports.SerialStre
at System.Threading.QueueUser
at System.Threading.Execution
at System.Runtime.CompilerSer
at System.Threading.Execution
at System.Threading.Execution
at System.Threading.QueueUser
at System.Threading.ThreadPoo
at System.Threading._ThreadPo
InnerException:
However I can send it to a MsgBox(indata) thats works..
What do I do wrong??
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.
Do something like this:
http://www.codeproject.com/Articles/31985/Updating-the-UI-from-a-thread-The-simplest-way
http://www.codeproject.com/Articles/31985/Updating-the-UI-from-a-thread-The-simplest-way
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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?
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?