Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 立即更新标签控件_C# - Fatal编程技术网

C# 立即更新标签控件

C# 立即更新标签控件,c#,C#,我的程序通过串口接收数据,我想通过标签控件显示数据。接收数据是连续更新的 如何立即刷新标签的值?以下是您必须执行的操作: 第一:在新线程中调用函数: Thread t = new Thread(new ThreadStart(yourfunction)); t.Start(); 第二:必须从表单中调用标签: void yourfunction() { while (..) // I think you have a loop in your function {

我的程序通过串口接收数据,我想通过标签控件显示数据。接收数据是连续更新的


如何立即刷新标签的值?

以下是您必须执行的操作:

第一:在新线程中调用函数:

Thread t = new Thread(new ThreadStart(yourfunction));
t.Start();
第二:必须从表单中调用标签:

void yourfunction()
{
    while (..) // I think you have a loop in your function
    {
        if (this.label1.InvokeRequired)
        {
            this.Invoke(
            new changeLabelDelegateMethod(changeLabelText), label1, "your text");
        }
        else
            label1.Text = "your text";
    }
}
delegate void changeLabelDelegateMethod(Label l, string text);
void changeLabelText(Label l, string text)
{
        l.Text = text;
}

你能说得更具体些吗?当您收到该值时,必须在label控件上设置该值并刷新label控件。很可能您的应用程序和串行端口通信在同一线程上,因此正在侦听和读取通过串行端口接收的数据的类正在阻塞UI。您可能应该将串行端口通信推送到后台线程。看看
BackgroundWorker
类。