Over ride closing event
-
**NOOB** I am over riding the closing event for the main form, Windows Shutdown process halts until the application is manually closed. Then I have to initiate the shutdown sequence again. Below I have the OnClosing event. I am looking to combine the two below so that they close out the application if Shutdown, Restart or Logoff are initiated. protected override void OnClosing(CancelEventArgs e) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; Hide(); } protected override void WndProc(ref Message m) { base.WndProc (ref m); if (m.Msg == 0x0011) { Application.Exit(); //this.Close(); } } If I have these 2 methods in place, the application WILL shutoff, BUT it will also halt the shutdown process, and once again I have to start the shutdown process all over again. Thank you in advance for your help. **DAN**
-
**NOOB** I am over riding the closing event for the main form, Windows Shutdown process halts until the application is manually closed. Then I have to initiate the shutdown sequence again. Below I have the OnClosing event. I am looking to combine the two below so that they close out the application if Shutdown, Restart or Logoff are initiated. protected override void OnClosing(CancelEventArgs e) { e.Cancel = true; this.WindowState = FormWindowState.Minimized; Hide(); } protected override void WndProc(ref Message m) { base.WndProc (ref m); if (m.Msg == 0x0011) { Application.Exit(); //this.Close(); } } If I have these 2 methods in place, the application WILL shutoff, BUT it will also halt the shutdown process, and once again I have to start the shutdown process all over again. Thank you in advance for your help. **DAN**
Calling
Application.Exit
sends theWM_QUIT
message to the application, this the pump catches and theWM_CLOSE
event is not (typically) called, soOnClosing
is not executed. That much you've discovered. When handling theWM_QUERYENDSESSION
notification message, though, you should setMessage.Result = new IntPtr(1);
in order to essentially returnTRUE
as the return value so that the shutdown process isn't canceled. By not setting this value,FALSE
is essentially returned and the shutdown process is cancelled as you have noticed. For more information, see the documentation for theWM_QUERYENDSESSION
notification message at http://msdn.microsoft.com/library/en-us/sysinfo/base/wm_queryendsession.asp[^] and pay close attention the return value documentation and the remarks section. Hopefully this will help, but it won't combine those two handlers. Honestly though, it really doesn't matter - it solves the problem and that's what counts. Besides, theClosing
event is triggered by theWM_CLOSE
message which isn't sent to Windows unless the application pump handles an appropriate message and sends or posts the message itself (at least, from what I remember).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----