Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The UI may only be updated by the main application thread. The async callback for the serial port event is handled behind the scenes on a separate thread. As Will mentioned, you can use Dispatcher.Invoke to queue a UI component property change on the UI thread.</p> <p>However, since you're using WPF, there's a more elegant &amp; idiomatic solution using bindings. Assuming that the data you receive on the serial port has some significant value to your business objects, you can have the DataReceived event update a property in an object and then bind the UI to that property.</p> <p>In rough code:</p> <pre><code>Public Class MySerialData Implements System.ComponentModel.INotifyPropertyChanged Public Event PropertyChanged(sender as Object, e as System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotfifyPropertyChanged.PropertyChanged private _serialData as String Public Property SerialData() As String Get Return _serialData End Get Set(value as String) If value &lt;&gt; _serialData Then _serialData = value RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("SerialData")) End If End Property </code></pre> <p>Then in your XAML file, you can bind the text box to this object property:</p> <pre><code>&lt;TextBox Text="{Binding Path=SerialData}"/&gt; </code></pre> <p>This assumes that the DataContext is set to an instance of your MySerialData class. The great thing about doing this extra bit of plumbing is that WPF will now handle all of the cross-thread marshaling for you automagically so you don't have to worry about which thread is invoking UI changes, the binding engine in WPF just makes it work. Obviously, if this is just a throw away project it may not be worth the extra bit of upfront code. However, if you are doing a lot of async communication and updating the UI this feature of WPF is a real life saver and eliminates a large class of bugs common to multi-threaded applications. We use WPF in a heavily threaded application doing a lot of TCP communication and the bindings in WPF were great especially when the data coming over the wire is meant to update several places in the UI since you don't have to have threading check sprinkled throughout your code.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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