Prevent windows Shutdown with CANCEL option C#

百般思念 提交于 2019-12-23 01:36:12

问题


I'm trying to include in my Windows Form App a way that when user tries to shutdown windows it opens a dialog box with CANCEL option....that office classic one.

In other topics, people describes how to prevent windows shutdown. They use a dialog box for this. It helps but if the user immediately clicks in any option in this box, windows closes the application.

You can understand what I'm meaning, doing the following test:

In windows Vista or 7 Open Paint, Word or any office soft and begin writing something. Do not save it.

Try to shutdown windows and when the classic save dialog box appears IMMEDIATELY click in "cancel".

You will see that the application continues to work and windows is asking you what you want to do.

I tried to follow this Microsoft Link but if I click "ok" in Message Box, the App closes.


回答1:


   private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
        {
           if (MessageBox.Show("You are closing this app.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
               return;    
           else    
               e.Cancel = true;
        }
    }



回答2:


Although delay windows shutdown is not recommended it is possible using shutdown scripts. You can configure your own script file using gpedit.msc configuration file. I have read that these scripts can delay the windows shutdown up to 10 minutes.

I hope it helps



来源:https://stackoverflow.com/questions/6454622/prevent-windows-shutdown-with-cancel-option-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!