Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6373209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:20:09+00:00 2026-05-25T01:20:09+00:00

I’m having an issue using a new WPF app that is trying to display

  • 0

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; }
  }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:20:10+00:00Added an answer on May 25, 2026 at 1:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am using Paperclip to handle profile photo uploads in my app. They upload
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.