简体   繁体   English

打开对话框可防止Windows关闭

[英]Open dialog box prevents windows shutdown

I have a C# GUI app that shows a message using MessageBox.Show(Message); 我有一个C#GUI应用程序,使用MessageBox.Show(Message);显示消息MessageBox.Show(Message); , however if the user fails to click on this, and then requests shutting down the PC, it blocks the shutdown. 但是,如果用户未能单击此按钮,然后请求关闭PC,则会阻止关闭。 How do I prevent my open dialog box from blocking the shutdown? 如何阻止我的打开对话框阻止关机?

I'm assuming you're using WinForms since you didn't mention WPF. 我假设你使用的是WinForms,因为你没有提到WPF。 You can't use a MessageBox if you want to control closing behavior. 如果要控制关闭行为,则无法使用MessageBox。 You'll have to build your own screen to act as a message box and use the ShowDialog method to display it. 您必须构建自己的屏幕以充当消息框并使用ShowDialog方法来显示它。 Your screen can handle the FormClosing event to detect when Windows is shutting down: 您的屏幕可以处理FormClosing事件以检测Windows何时关闭:

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.WindowsShutDown)
        {
            //...
        }
    }

So you'll want to allow the screen to close in this case and perhaps take other action for other types of close signals. 因此,您希望在这种情况下允许屏幕关闭,并且可能对其他类型的关闭信号采取其他操作。 To prevent the screen from closing, set the Cancel flag on the FormClosingEventArgs parameter to true; 要防止屏幕关闭,请将FormClosingEventArgs参数上的Cancel标志设置为true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM