How to delay or disable the "End Program" dialog when intercepting the shutdown?


  • I wanted to intercept user's logoff/shutdown to give the last chance to update the data for my application and it is working fine with the following code. But there is a minute annoyance. Five seconds after my form displayed intercepting logoff/shutdown event, the system pops up a dilaog which is basically asking "The program is not responding. Do you want to end it now?".


    How do I disable/delay this "End Program" dialog during the user is working with my form just before shutting down?


    And while I am posting, let me ask you guys another question: Is there anybody who knows how to detect whether user selected "Logoff" or "Shutdown" or "Restart" when the code received WM_QUERYENDSESSION?  I know how to tell between "Logoff" and "Shutdown/Restart" but what about between "Shutdown" and "Restart"?



    Private Const WM_QUERYENDSESSION As System.Int32 = &H11


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

       If m.Msg = WM_QUERYENDSESSION Then

          IsSystemShuttingDown = True


       End If


    End Sub

    Wednesday, May 14, 2008 7:52 PM


  • Windows doesn't like it when a window doesn't respond to its WM_QUERYENDSESSION message.  For all it knows, the application is really not responding.  You're not, you've blocked the message handling with ShowDialog().  There's not much you can do here but set m.Result to IntPtr.Zero (FALSE) so the shutdown is canceled and use Show().

    The user is not going to appreciate that, she ended the session for a reason.  Consider honoring her request and simply assume she wasn't interested in updating anything.  In fact, this got abused so much that Vista is no longer honoring a FALSE return and will shut down the PC anyway.

    Message.LParam contains the shutdown reason, check the MSDN library.

    Friday, May 16, 2008 2:36 AM