Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MessageBox挂起_C#_Winforms - Fatal编程技术网

C# MessageBox挂起

C# MessageBox挂起,c#,winforms,C#,Winforms,当按下winforms应用程序上的按钮时,我试图显示一个消息框,但消息框挂起并且从不返回值 private void btnApply_Click(object sender, EventArgs e) { bool current = false; if (cmbEmergencyLandingMode.SelectedIndex > 0) { if (m_WantedData != false)

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

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
        }
    }

感谢所有帮助过我的人

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
    {

    }
}

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

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
    }
}

你的代码应该能用…你确定吗?这应该行得通。。在我做了一些与这段代码无关的更改后,它开始挂起,整个应用程序都卡住了。你重建了你的应用程序吗?(F6)此外,您可能必须删除..\Projects\Debug文件夹中的旧.exe“在我做了一些与此代码无关的更改后,它开始挂起”,听起来这是您真正的问题,然后我也尝试了。仍然挂着。我甚至尝试重新启动计算机,因为它刚刚开始挂起,没有任何原因。任何控件验证事件中都有断点。当光标离开焦点时,它可能会挂起计算机。在这种情况下,按Ctrl+Alt+Del。这将导航到visual studio屏幕,您可以继续调试。当应用程序挂起时,按Ctrl+Alt+Del时会发生什么情况。如果您使用的是vista、win 7或win 8,请从列表中选择task manager。它可能会破坏当前进程。它说应用程序正在运行,因为它正在运行。消息框卡住了。我尝试了
this.BeginInvoke((操作)ApplyClick)但它不起作用