美好的一天。我有一张表格和一个背景 worker 。在 bw_Dowork 事件中,有些情况下我需要使用 MessageBox.Show()(即是?否框)打印消息。但是,每当我调用 messageBox.Show() 方法时,执行都会卡住并且表单不允许我单击我的选择(即是/否)。有时,如果我想工作,我必须按照消息显示的速度快速单击。否则,当我给出一秒钟的间隙时它会卡住。我使用 MessageBox.Show() 的实例示例如下所示:
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
if (fileFTP.Exists == false)
{
_busy.WaitOne();
if ((worker.CancellationPending == true))
{
e.Cancel = true;
return;
}
SetText("File Ftp.exe are missing. This file are required to preform update, please contact yout system administrator to retrive Ftp.exe", true);
MessageBox.Show("File Ftp.exe are missing. This file are required to preform update, please contact yout system administrator to retrive Ftp.exe");
goto ExitProgram;
}
}
我在网上对此做了一些研究,有人建议 MessageBox 干扰了界面线程。这使我使用委托(delegate)触发消息,但都无济于事。我不得不删除所有的消息框。离开一个仍然会在被解雇时卡住我的执行。请提供任何帮助。
最佳答案
尝试使用主 UI 线程来显示消息框:
this.BeginInvoke((Action)(() => MessageBox.Show("Hello")));
(假设this 是一个表单)
顺便说一句:“转到”?严重吗?
关于c# - MessageBox.Show() 卡住执行 : windows form c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856017/