Note that there are some explanatory texts on larger screens.

plurals
  1. POStreamming high-speed data to WPF UI using threading and MVVM
    primarykey
    data
    text
    <p>I'm having an issue using a new WPF app that is trying to display a high-speed stream of bytes to a textbox. The bytes come over a serial port, I've made an older WinForms app that handled the stream just fine, had a background thread that read from the serial port and posted to the UI via invoking a delegate. </p> <p>Now the issue with this WPF app is that I'm using the basic MVVM approach. I have the textbox on the UI bound to a property on the VM that fires PropertyChanged event based on INotifyPropertyChanged. When the data is ready to post to the ui via a subscribed event on the service reading the serial port, I use the following:</p> <pre><code>Action dispatchAction = () =&gt; { FormattedStream += s; }; _currentDispatcher.Invoke(dispatchAction); </code></pre> <p>FormattedStream being the VM string property that the UI binds to.</p> <p>What's happening on the WPF app that isn't happening in the WinForms version is the WPF app becomes slow and unresponsive as it's running along with the fact it can't keep up with the stream as well as the WinForms app and the wpf app is using/requiring more of the processor according to my task manager.</p> <p>What I'm wanting to know is if there is some solution out there to handle streaming (high-speed) data to a WPF UI.</p> <p>ETA: I also tried using the BeginInvoke instead of Invoke and when using BeginInvoke the stream last for a few seconds then freezes. Invoke is the only way I could get it to continuously stream to the UI.</p> <p>ETA: Here's the code:</p> <p>// The Window/View</p> <pre><code>public partial class MainWindow : Window, IView { public MainWindow() { InitializeComponent(); } public IViewModel ViewModel { get { return DataContext as IViewModel; } set { DataContext = value; } } public void ScrollToCaret() { txtBoxOutPut.ScrollToEnd(); if (txtBoxOutPut.Text.Length &gt; 10000) txtBoxOutPut.Text = txtBoxOutPut.Text.Remove(0, 9000); } public event Action ComPortSelected; public event Action StartPortReader; public event Action StopPortReader; private void Start_Click(object sender, RoutedEventArgs e) { StartPortReader.Invoke(); } private void Stop_Click(object sender, RoutedEventArgs e) { StopPortReader.Invoke(); } </code></pre> <p>}</p> <p>// The ViewModel</p> <pre><code>public class ViewModel : IViewModel, INotifyPropertyChanged </code></pre> <p>{ private readonly ISerialPortReaderService _portReaderService; private readonly Dispatcher _currentDispatcher;</p> <pre><code> public ViewModel(IView view, ISerialPortReaderService portReaderService) { View = view; View.ViewModel = this; View.StartPortReader += View_StartPortReader; View.StopPortReader += View_StopPortReader; _portReaderService = portReaderService; _currentDispatcher = Dispatcher.CurrentDispatcher; _portReaderService.ByteArrived += _portReaderService_ByteArrived; } private void _portReaderService_ByteArrived(string s) { Action dispatchAction = () =&gt; { FormattedStream = s; }; _currentDispatcher.Invoke(dispatchAction); } private void View_StopPortReader() { _portReaderService.Stop(); } private void View_StartPortReader() { _portReaderService.Start(SelectedPort); } public IView View { get; private set; } public void ShowView() { View.Show(); } private StringBuilder _FormattedStream = new StringBuilder(); public string FormattedStream { get { return _FormattedStream.ToString(); } set { _FormattedStream.Append(value); PropertyChanged(this, new PropertyChangedEventArgs("FormattedStream")); View.ScrollToCaret(); } } private string _SelectedPort; public string SelectedPort { get { return _SelectedPort; } set { _SelectedPort = value; PropertyChanged(this, new PropertyChangedEventArgs("SelectedPort")); } } public ReadOnlyCollection&lt;string&gt; AvailablePorts { get { return GetAvailablePorts(); } } private ReadOnlyCollection&lt;string&gt; GetAvailablePorts() { var ports = System.IO.Ports.SerialPort.GetPortNames(); return new ReadOnlyCollection&lt;string&gt;(ports.ToList()); } public event PropertyChangedEventHandler PropertyChanged = delegate { }; </code></pre> <p>}</p> <p>// Serial Port reader service</p> <pre><code>public class SerialPortReaderService : ISerialPortReaderService </code></pre> <p>{ private SerialPort _port = new SerialPort(); private readonly IThreadRunner _threadRunner;</p> <pre><code> public SerialPortReaderService(IThreadRunner threadRunner) { _threadRunner = threadRunner; } public void Start(string comPort) { if (_port != null &amp;&amp; !_port.IsOpen) { _port.PortName = comPort; _port.BaudRate = 4800; _port.Open(); _threadRunner.Start(() =&gt; { var b = new byte[20]; var bArray = _port.Read(b, 0, 20); foreach (var b1 in b) { next10Bytes.Append(b1 + ", "); } BytesArrived(next10Bytes.ToString()); next10Bytes.Clear(); Thread.Sleep(10); }); } } private StringBuilder next10Bytes = new StringBuilder(); public void Stop() { if (_port.IsOpen) { _threadRunner.Stop(); _port.Close(); } } public event Action&lt;string&gt; BytesArrived; </code></pre> <p>}</p> <p>// A threadrunner I use</p> <pre><code>public class ThreadRunner : IThreadRunner </code></pre> <p>{ private Thread _thread; private bool _isRunning;</p> <pre><code> /// &lt;summary&gt; /// Will continuously run in a while loop the action submitted in a separate thread /// &lt;/summary&gt; /// &lt;param name="toDoAction"&gt;&lt;/param&gt; public void Start(Action toDoAction) { if (_thread != null &amp;&amp; _thread.IsAlive) Stop(); _isRunning = true; _thread = new Thread(() =&gt; { while (_isRunning) { toDoAction.Invoke(); } }); _thread.Start(); } public void Stop() { _isRunning = false; if (_thread != null &amp;&amp; _thread.IsAlive) { _thread.Abort(); _thread.Join(new TimeSpan(0, 0, 1)); } } public bool ThreadIsRunning { get { return _isRunning; } } </code></pre> <p>}</p>
    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.
 

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