MessageBox.Show() 函数会冻结程序的执行: Windows Form C#

4

你好。我有一个表单和一个后台工作程序。在bw_DoWork事件中,有时需要使用MessageBox.Show()方法打印消息(即YES?NO框)。但是,每当我调用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干扰了界面线程。这让我使用委托来触发消息,但都没有成功。我不得不删除所有的MessageBoxes。留下一个仍会在触发时冻结我的执行。请帮帮忙,谢谢。


听起来很奇怪。对我来说没有冻结。 - King King
1个回答

9

尝试使用主 UI 线程显示消息框:

this.BeginInvoke((Action)(() => MessageBox.Show("Hello")));

(假设这是一个表单
顺便说一下:“goto”?真的吗?

4
我看到 goto 的时候也是这么想的! :D - Abbas
虽然我用了不同的方法,但你的方法也可以。谢谢。 - Nuru Salihu
2
我以为所有的goto "Users"早就消失了。:p - Ayub Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接