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.
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:
Action dispatchAction = () => { FormattedStream += s; };
_currentDispatcher.Invoke(dispatchAction);
FormattedStream being the VM string property that the UI binds to.
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.
What I’m wanting to know is if there is some solution out there to handle streaming (high-speed) data to a WPF UI.
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.
ETA: Here’s the code:
// The Window/View
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 > 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();
}
}
// The ViewModel
public class ViewModel : IViewModel, INotifyPropertyChanged
{
private readonly ISerialPortReaderService _portReaderService;
private readonly Dispatcher _currentDispatcher;
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 = () => { 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<string> AvailablePorts
{
get { return GetAvailablePorts(); }
}
private ReadOnlyCollection<string> GetAvailablePorts()
{
var ports = System.IO.Ports.SerialPort.GetPortNames();
return new ReadOnlyCollection<string>(ports.ToList());
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
// Serial Port reader service
public class SerialPortReaderService : ISerialPortReaderService
{
private SerialPort _port = new SerialPort();
private readonly IThreadRunner _threadRunner;
public SerialPortReaderService(IThreadRunner threadRunner)
{
_threadRunner = threadRunner;
}
public void Start(string comPort)
{
if (_port != null && !_port.IsOpen)
{
_port.PortName = comPort;
_port.BaudRate = 4800;
_port.Open();
_threadRunner.Start(() =>
{
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<string> BytesArrived;
}
// A threadrunner I use
public class ThreadRunner : IThreadRunner
{
private Thread _thread;
private bool _isRunning;
/// <summary>
/// Will continuously run in a while loop the action submitted in a separate thread
/// </summary>
/// <param name="toDoAction"></param>
public void Start(Action toDoAction)
{
if (_thread != null && _thread.IsAlive)
Stop();
_isRunning = true;
_thread = new Thread(() =>
{
while (_isRunning)
{
toDoAction.Invoke();
}
});
_thread.Start();
}
public void Stop()
{
_isRunning = false;
if (_thread != null && _thread.IsAlive)
{
_thread.Abort();
_thread.Join(new TimeSpan(0, 0, 1));
}
}
public bool ThreadIsRunning
{
get { return _isRunning; }
}
}
From what Petoj mentioned I made a new window, still using the streaming service but had just the window itself subscribe to the bytesarrived event and manually append to the txtbox via the Dispatcher.Invoke and low and behold that solved the issue, no slow down and no massive CPU usage.
Moral of the story is that binding or at least binding to a string constantly being new’d up each time it was added to was causing the slow down.