更新的用户界面控制,在运行的WinForms后台线程 [英] update a control in UI with running background Thread in Winforms

查看:115
本文介绍了更新的用户界面控制,在运行的WinForms后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的后台工作线程在我的WinForm的,我Do_Work事件中,我计算的东西,我需要的东西是,同时我想更新的标签是主/ UI线程?如何实现这一目标?

I am using Background Worker Thread in my Winform, inside my Do_Work Event I am calculating something, what I need the thing is that simultaneously I would like to update a label which is in main/UI thread ? how to achieve this?

我想更新从Do_Work事件我的标签...

I want to update my label from Do_Work event...

推荐答案

在的WinForms(WPF以及)UI控件只能在UI线程更新。您应该更新您的标签是这样的:

In WinForms (WPF as well) UI controls can only be updated in the UI thread. You should update your label this way:

public void UpdateLabel(String text){
    if (label.InvokeRequired)
    {
        label.Invoke(new Action<string>(UpdateLabel), text);
        return;
    }      
    label.Text = text;
}

这篇关于更新的用户界面控制,在运行的WinForms后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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