Slow WPF Text Box

I am developing a simple serial data viewer that will be used to view data transmitted to one of the serial ports of a computer. I wrote a test application using C # and WPF; it just puts the last line read in a text block. However, it skips all the other lines. My theory is that new data is placed in a text block before WPF displays this window. However, I tried every combination of thread priorities that I can think of, and, at best, the application shows every other line; in the worst case, it shows every 20 lines.

I am running on a multi-core computer. My application consists of a text block and a button for opening / closing a port. (I tried replacing the text block with a text field, and I am observing 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 handling the steady bandwidth of large amounts of data; Is WPF appropriate in this situation? If so, what am I doing wrong?

+3
source share
2 answers

One of my company products shows "almost real-time" data updates from the server, and there are a few things you can try ....

, . , .

:

:

public static readonly DependencyProperty MessageTextProperty = 
    DependencyProperty.Register("MessageText", typeof(string), typeof(MyWidowClass), 
    new UIPropertyMetadata(string.Empty));

        public string MessageText
        {
            get { return (int)GetValue(MessageTextProperty ); }
            set { SetValue(MessageTextProperty , value); }
        }

xaml:

<TextBox Text="{Binding Path=MessageText, ElementName=xNameOfMyWindow}"/>

xNameOfMyWindow - x: Name

:

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

, , :

:

TextWrapping = "Nowrap"

, , , , , Environment.NewLine, , , .

, .

+2

Source: https://habr.com/ru/post/1775890/


All Articles