Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>WELCOME TO THE MAGICAL WORLD OF MULTITHREADING!!!</strong></p> <p>What's happening is that all of your UI elements (class instances) can only be accessed/updated by the UI thread. I won't go into the details about this thread affinity, but its an important subject and you should check it out.</p> <p>The event where data is coming in on the serial port is happening on a <em>different thread than the UI thread</em>. The UI thread has a message pump that handles windows messages (like mouse clicks etc). Your serial port doesn't send off windows messages. When data comes in on the serial port a completely different thread from your UI thread is used to process that message.</p> <p>So, within your application, the mSerialPort_DataReceived method is executing in a different thread than your UI thread. You can use the Threads debugging window to verify this.</p> <p>When you attempt to update your UI, you are trying to modify a control with thread affinity for the UI thread from a different thread, which throws the exception you've seen.</p> <p>TL;DR: You are trying to modify a UI element outside of the UI thread. Use </p> <pre><code>txtSerialOutput.Dispatcher.Invoke </code></pre> <p>to run your update on the UI thread. <a href="http://msdn.microsoft.com/en-us/library/ms591596(VS.85).aspx" rel="noreferrer">There's an example here on how to do thisin the community content of this page.</a></p> <p>The Dispatcher will Invoke your method on the UI thread (it sends a windows message to the UI saying "Hai guize, run this method kthx"), and your method can then safely update the UI from the UI thread.</p>
 

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