Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow WPF Textbox

I am developing a simple serial data viewer that will be used to watch the data being transmitted to one of a computer's serial ports. I wrote a test application using C# and WPF; it simply places the most recently read line into a textblock. However, it skips every-other line. My theory is that new data is being put into the textblock before WPF renders the window. However, I've tried every combination of thread priorities I can think of and, at best, the application shows every other line; at worst, it shows every 20 lines.

I am running on a multicore computer. My application consists of a textblock and a button to open/close the port. (I have tried replacing the textblock with a textbox, and I observe the same problem)

My DataReceived handler:

private void MainWindow_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    string message = sp.ReadLine();
    if (string.IsNullOrWhiteSpace(message))
        return;

    this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate()
    {
        text.Text = message;
        this.InvalidateVisual();
    });
}

The highest priority for this application is to handle sustained throughput of a lot of data; is WPF appropriate in this situation? If it is, what am I doing wrong?

like image 787
CWMan Avatar asked Oct 26 '25 15:10

CWMan


1 Answers

I realize this is really late to the game here, but after struggling with this issue for about a month now, I stumbled upon the source of my problems with slow textbox updating:

Turning off textwrapping completely removed my UI locking problem:

TextWrapping="NoWrap"

This, of course, will mean that you'll need to be more responsible for ensuring your strings are wrapped properly before updating the textbox via Environment.NewLine, but its a small price to pay in my opinion.

Hope this helps.

like image 86
CAPGuy Avatar answered Oct 29 '25 03:10

CAPGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!