C#如何在单击消息框按钮之前停止执行 [英] C# how to stop execution until message box button is clicked

查看:130
本文介绍了C#如何在单击消息框按钮之前停止执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序从我的.dll中捕获事件消息。

然后显示MessageBox。然后退出该程序。

但是,您可以忽略MessageBox按钮并继续使用该程序。

所以我要禁用该程序,直到我按下OK按钮。

我想禁用该程序。



我尝试过:



My program catches event msg from my .dll.
It then shows the MessageBox. Then exit the program.
However, you can ignore the MessageBox button and continue to use the program.
So I want to disable the program until I press the OK button.
I would like to disable the program.

What I have tried:

private void ExitProgram(object sender, EventArgs e)
        {
            string msg = sender as string;
            Log.Instance.InsertLogError(msg);

            DateTime nowTime = DateTime.Now;

            while (Log.Instance.GetLogQueueCount() != 0 || nowTime.AddSeconds(10) < DateTime.Now)
            {
                Thread.Sleep(100);
            }
            Log.Instance.Dispose();

            MyProgramtWindowsModeErr(msg);
            //if (Program.bWindowMode == true)
            //    MessageBox.Show(msg, "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Error);

            (Process.GetCurrentProcess()).Kill();
        }


        public bool MyProgramtWindowsModeErr(string msg)
        {
            if (Program.bWindowMode == true)
            {
                MessageBox.Show(msg, "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }





在MyProgramtWindowsMo​​deErr中,我检查了true并相互贴身。



但是,我无法打开消息框或继续使用该程序。

帮帮我PLZ ..



In MyProgramtWindowsModeErr, I checked true and fasle for each other.

However, I could not open the message box or continue using the program.
Help me plz..

推荐答案

您所要做的就是显示一个模态对话框,执行将暂停,直到您关闭对话框。恰好,MessageBox *是* modal。当然,我假设您没有运行任何异步进程或以其他方式运行多个线程。
All you have to do is display a modal dialog box, and execution will pause until you dismiss the dialog. It just so happens that MessageBox *is* modal. Of course, I'm assuming that you're not running any async processes or otherwise running multiple threads.


public bool MyProgramtWindowsModeErr(string msg)
        {
            if (Program.bWindowMode == true)
            {
                if (DialogResult.OK == MessageBox.Show(msg, "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Error))
                {
                    return false;
                }
            }
            return true;
        }


这篇关于C#如何在单击消息框按钮之前停止执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆