运行时更新标签 [英] Update a label while running

查看:41
本文介绍了运行时更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的实时应用程序来使用WinForms测试GPS模块。我想将GPS中的当前数据连续发送到标签或文本框以验证性能。我已经尝试了一个简单的程序来测试如何执行此操作,如下所示:



I am building a simple real time app to test a GPS module using WinForms. I would like to send current data from the GPS to a label or text box continuously to validate performance. I have tried a simple program to test how to do this, shown below:

    public partial class Form1 : Form
    {
        public Form1()
        {
            int i;
            string s;
            InitializeComponent();
            for (i=0; i<20; i++)
            {
                s = String.Format("Curent value of i {0}", i);
                //                FirstLabel.Text = "This is a test";
                FirstLabel.Text = s;
                FirstLabel.Refresh();

                TextBox.Text = s;
                TextBox.Refresh();
                
                
                Thread.Sleep(1000);
             
            }
//            FirstLabel.Text = "This is a test";
//            Thread.Sleep(1000);
//            FirstLabel.Text = "Hello World";
        }





当我这样做时,winform屏幕仅在循环完成后显示,显示最后一个字符串i = 19。我想要发生的是连续显示winform并使每个框的内容每秒从0增加到19。我查看了很多帖子,并想到了TextBox.Refresh();会诀窍,但显然不是。应该很简单,但我无法弄清楚该怎么做。任何帮助将不胜感激。



我尝试过:



我上面尝试过的内容。



When I do this the winform screen only shows up after the loop is complete with the last string i=19 displayed. What I want to happen is to display the winform continuously and have the contents of the boxes increment from 0 to 19 once each second. I have looked through a lot of posts and thought the TextBox.Refresh(); would to the trick but evidently not. Should be really simple but I can't figure out what to do. Any help would be greatly appreciated.

What I have tried:

What I have tried described above.

推荐答案

那是因为你的代码占用了UI(启动)线程。当UI线程忙于运行代码时,它无法处理应用程序消息泵并且无法重新绘制任何内容,包括您的标签。



您必须将工作移至后台线程,任务或BackgroundWorker。这将释放UI线程以维护UI。您的后台代码必须编组回调到UI线程以更新标签等内容。



这个 [ ^ ]。
That's because your code is hogging the UI (startup) thread. While the UI thread is busy running your code it cannot process the application message pump and cannot repaint anything, including your label.

You have to move your work to a background thread, a Task or BackgroundWorker. This will free up the UI thread to maintain the UI. Your background code has to marshal called back to the UI thread to update things like your label.

Something like this[^].


使用 Application.DoEvents方法(System.Windows.Forms) [ ^ ]

use Application.DoEvents Method (System.Windows.Forms)[^]
 TextBox.Text = s;
Application.DoEvents();


对于这种类型的东西,也许另一个线程是矫枉过正的。查看向表单添加计时器,以1秒的间隔触发。这样,当你激活时你仍然在UI线程上,所以你不需要编组标签文本的设置,你不需要做任何Sleep调用来延迟继续,因为这个调用只是按所选间隔开除。
For this type of thing, perhaps another thread is overkill. Look at adding a timer to the form, that fires at an interval of 1 second. That way, you are still on the UI thread when it is activated, so yu don't need to marshal the setting of the label text, and you don't need to do any Sleep calls to delay proceeding, as the call is only fired at the selected interval.


这篇关于运行时更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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