慢 WPF 文本框 [英] Slow WPF Textbox

查看:66
本文介绍了慢 WPF 文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的串行数据查看器,用于观察传输到计算机串行端口之一的数据.我使用 C# 和 WPF 编写了一个测试应用程序;它只是将最近阅读的行放入文本块中.但是,它会跳过每隔一行.我的理论是在 WPF 呈现窗口之前将新数据放入文本块.但是,我已经尝试了我能想到的所有线程优先级组合,而且,应用程序最多显示每隔一行;最坏的情况是每 20 行显示一次.

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)

我的 DataReceived 处理程序:

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

此应用程序的最高优先级是处理大量数据的持续吞吐量;WPF 在这种情况下是否合适?如果是,我做错了什么?

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?

推荐答案

我公司的一个产品显示来自服务器的近乎实时"的数据更新,您可以尝试以下几种方法....

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....

如果您对 text.Text 进行数据绑定而不是直接设置它,您或许可以将它移到调度程序调用之外.

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

你可以这样做:

添加依赖属性:

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 文本框中:

on your xaml textbox:

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

其中 xNameOfMyWindow 是窗口标签的 x:Name 属性

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

这篇关于慢 WPF 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆