Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialPort reading cause error because of not owning thread
    primarykey
    data
    text
    <p>I have a simple WPF windows application trying to read a serial port with the <code>System.IO.Ports.SerialPort</code>. </p> <p>When I try to read the incoming data in the <code>DataReceived</code> event, I get an exception saying that I don't have access to the thread. How do I solve it?</p> <p>I have this in the WPF window class:</p> <pre><code>Public WithEvents mSerialPort As New SerialPort() Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnConnect.Click With mSerialPort If .IsOpen Then .Close() End If .BaudRate = 4800 .PortName = SerialPort.GetPortNames()(0) .Parity = Parity.None .DataBits = 8 .StopBits = StopBits.One .NewLine = vbCrLf .Open() End With End Sub Private Sub mSerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles mSerialPort.DataReceived If e.EventType = SerialData.Chars Then txtSerialOutput.Text += mSerialPort.ReadExisting() End If End Sub Protected Overrides Sub Finalize() If mSerialPort.IsOpen Then mSerialPort.Close() End If mSerialPort.Dispose() mSerialPort = Nothing MyBase.Finalize() End Sub </code></pre> <p>When the <code>DataReceived</code> event triggers, I get the following exception on <code>mSerialPort.ReadExisting()</code> :</p> <pre><code>System.InvalidOperationException was unhandled Message="The calling thread cannot access this object because a different thread owns it." Source="WindowsBase" StackTrace: at System.Windows.Threading.Dispatcher.VerifyAccess() at System.Windows.Threading.DispatcherObject.VerifyAccess() at System.Windows.DependencyObject.GetValue(DependencyProperty dp) at System.Windows.Controls.TextBox.get_Text() at Serial.Serial.mSerialPort_DataReceived(Object sender, SerialDataReceivedEventArgs e) in D:\SubVersion\VisionLite\Serial\Serial.xaml.vb:line 24 at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e) at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state) at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack) at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload