T
Terry Olsen
I run this code:
Private Sub p_recv(ByVal sender As Object, ByVal e As
SerialReceivedEventArgs) Handles p.ReceivedEvent
txtRecv.Text += p.ReadExisting
End Sub
I get this error:
--------------------ERROR TEXT--------------------
Illegal cross-thread operation: Control 'txtRecv' accessed from a thread
other than the thread it was created on.
Stack trace where the illegal operation occurred was:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_WindowText()
at System.Windows.Forms.TextBoxBase.get_WindowText()
at System.Windows.Forms.Control.get_Text()
at System.Windows.Forms.TextBoxBase.get_Text()
at System.Windows.Forms.TextBox.get_Text()
at ComPortTest.Form1.p_recv(Object, SerialReceivedEventArgs)
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object,
SerialReceivedEventArgs)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object)
at System.Threading.ExecutionContext.Run(ExecutionContext, ContextCallback,
Object, StackCrawlMark&)
at System.Threading._ThreadPoolWaitCallback.WaitCallback(Object)
---------------END ERROR TEXT-----------------------
Evidently, the function I'm calling creates a thread. So how can I write to
the textbox from the function without getting this error?
Private Sub p_recv(ByVal sender As Object, ByVal e As
SerialReceivedEventArgs) Handles p.ReceivedEvent
txtRecv.Text += p.ReadExisting
End Sub
I get this error:
--------------------ERROR TEXT--------------------
Illegal cross-thread operation: Control 'txtRecv' accessed from a thread
other than the thread it was created on.
Stack trace where the illegal operation occurred was:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_WindowText()
at System.Windows.Forms.TextBoxBase.get_WindowText()
at System.Windows.Forms.Control.get_Text()
at System.Windows.Forms.TextBoxBase.get_Text()
at System.Windows.Forms.TextBox.get_Text()
at ComPortTest.Form1.p_recv(Object, SerialReceivedEventArgs)
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object,
SerialReceivedEventArgs)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object)
at System.Threading.ExecutionContext.Run(ExecutionContext, ContextCallback,
Object, StackCrawlMark&)
at System.Threading._ThreadPoolWaitCallback.WaitCallback(Object)
---------------END ERROR TEXT-----------------------
Evidently, the function I'm calling creates a thread. So how can I write to
the textbox from the function without getting this error?