【问题标题】:MessageBox hangs消息框挂起
【发布时间】:2014-07-16 06:14:19
【问题描述】:

我试图在按下 winforms 应用程序上的按钮时显示消息框,但 MessageBox 挂起并且从不返回值。

private void btnApply_Click(object sender, EventArgs e)
    {
        bool current = false;
        if (cmbEmergencyLandingMode.SelectedIndex > 0)
        {
            if (m_WantedData != false)
            {
                DialogResult dr = MessageBox.Show("Are you sure you want to enable Emergency Landing mode?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                //i never get to this point
                current = (dr == DialogResult.Yes);
            }
        }

        if (m_WantedData == current)
        {
            //do something
        }
        else if (m_WantedData != null)
        { 
            //do something else
        }
    }

编辑:好的,所以我通过处理后台工作人员上的按钮事件来让它工作:

private void btnApply_Click(object sender, EventArgs e)
    {
        if (!bwApply.IsBusy) 
            bwApply.RunWorkerAsync(cmbEmergencyLandingMode.SelectedIndex);
    }

    void bwApply_DoWork(object sender, DoWorkEventArgs e)
    {
        bool current = false;
        int selectedIndex = (int)e.Argument;

        if (selectedIndex > 0)
        {
            if (m_WantedData != false)
            {
                DialogResult dr = MessageBox.Show(
                    "Are you sure you want to enable Emergency Landing mode?",
                    "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                current = (dr == DialogResult.Yes);
            }
        }

        if (m_WantedData == current)
        {
            //do something
        }
        else if (m_WantedData != null)
        {
            //do something else
        }
    }

感谢所有帮助过的人!

【问题讨论】:

  • 你的代码应该可以工作...
  • 它工作正常,在我做了一些与这段代码无关的更改后,它开始挂起,整个应用程序卡住了。
  • 你重建你的应用了吗? (F6) 此外,您可能需要删除 ..\Projects\Debug 文件夹中的旧 .exe 文件
  • 在我做了一些与这段代码无关的更改后,它开始挂起”,听起来那是你真正的问题

标签: c# winforms


【解决方案1】:

您是否尝试过指定消息框所有者,如下所示?

DialogResult dr = MessageBox.Show(
    this,
    "Are you sure you want to enable Emergency Landing mode?",
    "Warning!",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Warning);

我见过一个应用程序在显示消息框的同时对消息框后面的 UI 进行并发更改时的行为是这样的。如果上述方法没有帮助,请尝试通过异步处理事件来减轻 UI 处理(即 WinAPI 消息传递)的压力,如下所示。

public delegate void ApplyDelegate();

private void btnApply_Click(object sender, EventArgs e)
{
   btnApply.BeginInvoke(new ApplyDelegate(ApplyAsync));
}

private void ApplyAsync()
{
    bool current = false;
    if (cmbEmergencyLandingMode.SelectedIndex > 0)
    {
        if (m_WantedData != false)
        {
            DialogResult dr = MessageBox.Show(
                this,
                "Are you sure you want to enable Emergency Landing mode?",
                "Warning!",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            //i never get to this point
            current = (dr == DialogResult.Yes);
        }
    }

    if (m_WantedData == current)
    {
        //do something
    }
    else if (m_WantedData != null)
    { 
        //do something else
    }
}

【讨论】:

  • 我试过this.BeginInvoke((Action)ApplyClick);但没用
【解决方案2】:

尝试在 MessageBox.Show() 方法中不指定父表单的情况下显示消息。我不知道为什么,但是当我加载模态窗口时遇到了类似的问题。

if (m_WantedData != false)
{
    if (MessageBox.Show("Sample Message", "Are you sure you want to enable Emergency Landing mode?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    {
        //DO YOUR STUFF HERE IF YES
    }
    else
    {

    }
}

【讨论】:

  • 我也试过了。仍然挂起。我什至尝试重新启动计算机,因为它刚刚开始无故挂起
  • 控制验证事件中是否有断点。当光标离开焦点时,它可能会挂起您的计算机。在这种情况下,请按Ctrl + Alt + Del。这将导航 Visual Studio 屏幕,您可以继续调试。
  • 当您在应用程序挂起时按Ctrl + Alt + Del 会发生什么。如果您使用的是 vista、win 7 或 win 8,请从列表中选择任务管理器。它可能会破坏当前进程。
  • 它说应用程序正在运行,因为它正在运行。它是卡住的消息框。
猜你喜欢
  • 2018-09-24
  • 2013-03-27
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多