Man*_*ish 1 .net c# multithreading
在C#.NET Windows应用程序(winforms)中,我将复选框的可见性设置为false:
checkBoxLaunch.Visible = true;
Run Code Online (Sandbox Code Playgroud)
我开始了一个帖子.
Thread th = new Thread(new ThreadStart(PerformAction));
th.IsBackground = true;
th.Start();
Run Code Online (Sandbox Code Playgroud)
该线程执行一些操作并将可见性设置为true:
private void PerformAction()
{
/*
.
.// some actions.
*/
checkBoxLaunch.Visible = true;
}
Run Code Online (Sandbox Code Playgroud)
线程完成任务后,我看不到该复选框.
我错过了什么?
您不应在非UI线程中进行UI更改.使用Control.Invoke,Control.BeginInvoke或BackgroundWorker当元帅的回调到UI线程.例如(假设C#3):
private void PerformAction()
{
/*
.
.// some actions.
*/
MethodInvoker action = () => checkBoxLaunch.Visible = true;
checkBoxLaunch.BeginInvoke(action);
}
Run Code Online (Sandbox Code Playgroud)
搜索Control.Invoke,Control.BeginInvoke或BackgroundWorker中的任何一个,以查找有关此内容的数百篇文章.