32

I have a Windows Forms application that when the Main Window is closing, it displays a basic dialog box, confirming the action. If the user decides to cancel the application exit is cancelled.

However, when the application is running minimized and the user wants to shut down the PC, the shutdown sequence stops because my application is waiting on the user to confirm the application close (the dialog box is displayed).

I thought of adding a timer for making a timeout and if no answer comes in a certain amount of time, close the application automatically, but even if this is a way to do it, it's certainly NOT how every other app does it.

So what would there be an optimal solution to confirm the application shutdown in every other case, unless the system is shutting down?

Thank you!

default locale
  • 13,035
  • 13
  • 56
  • 62
Jano Rajmond
  • 455
  • 6
  • 10

5 Answers5

54

In your FormClosing event check the FormClosingEventArgs' CloseReason property to see why the window is closing down. If it is CloseReason.WindowsShutDown then don't show your dialog and do not cancel the closing of your form.

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    // Verify that we're not being closed because windows is shutting down.
    if (e.CloseReason != CloseReason.WindowsShutDown)
    {
        // Show your dialog / cancel closing. 
    }
}

N.B: You might also want to include CloseReason.TaskManagerClosing as the user clearly wants to close your application in that scenario and the taskmanager already asks for confirmation. Or alternatively only show your dialog for CloseReason.UserClosing.

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
10

In the Closing event handler, which you can define like this:

    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);

and where I guess you are posting your confirmation dialog, you can check theCloseReason argument, and not post the dialog if it is the shutdown that causes it:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.WindowsShutDown)
        {
            //do not show the dialog
        }
    }
zmilojko
  • 2,125
  • 17
  • 27
6

SystemEvents can help you. The SessionEnding occurs when the user is trying to log off or shut down the system.

Microsoft.Win32.SystemEvents.SessionEnding += (sender, e) => DoYourJob();
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
  • Ditto, useful for my situation - provide a "replacement" UI to an app that lacks validation, and the PC is so locked down you can't restart the app if you close it. – Roger Willcocks Mar 06 '14 at 00:23
1

you can use Application.SessionEnding Event to understand if the system is about to shutdown

http://msdn.microsoft.com/en-us/library/system.windows.application.sessionending.aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

You could listen for the shutdown event and quit the application without a message box.

H-Man2
  • 3,169
  • 20
  • 19