简体   繁体   English

线程:设置复选框的可见性

[英]Threading: Setting checkbox's visibility

In a C#.NET windows application (winforms) I set the visibility of the checkbox to false: 在C#.NET Windows应用程序(winforms)中,我将复选框的可见性设置为false:

checkBoxLaunch.Visible = true;

I started a thread. 我开始了一个话题。

Thread th = new Thread(new ThreadStart(PerformAction));
th.IsBackground = true;
th.Start();

The thread performs some stuff and sets the visibility to true: 该线程执行一些操作并将可见性设置为true:

private void PerformAction()
{
/*
.
.// some actions.
*/
    checkBoxLaunch.Visible = true;

}

After the thread finishes its task, the checkbox is not visible to me. 线程完成其任务后,该复选框对我不可见。

What am I missing? 我想念什么?

You shouldn't make UI changes within a non-UI thread. 您不应该在非UI线程内进行UI更改。 Use Control.Invoke , Control.BeginInvoke or BackgroundWorker to marshal the call back to the UI thread. 使用Control.InvokeControl.BeginInvokeBackgroundWorker将呼叫Control.BeginInvoke送回UI线程。 For example (assuming C# 3): 例如(假设C#3):

private void PerformAction()
{
/*
.
.// some actions.
*/
    MethodInvoker action = () => checkBoxLaunch.Visible = true;
    checkBoxLaunch.BeginInvoke(action);
}

Search for any of Control.Invoke, Control.BeginInvoke or BackgroundWorker to find hundreds of articles about this. 搜索Control.Invoke,Control.BeginInvoke或BackgroundWorker中的任何内容,以查找有关此内容的数百篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM