Slow WPF Textbox

两盒软妹~` 提交于 2020-01-14 19:25:08

问题


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?


回答1:


One of my companies products is displaying "near real-time" updates of data from a server, and there are a couple of things you can try....

You might be able to move your text.Text outside of the dispatcher call if you databind it instead of directly setting it.

you can do this like so:

add a dependency property:

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

on your xaml textbox:

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

where xNameOfMyWindow is the x:Name attribute of your window tag

now your code would like this:

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



回答2:


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.



来源:https://stackoverflow.com/questions/4228289/slow-wpf-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!