Is Windows Shutting Down
-
Is there a way I can check to see if the OS is getting shut down within my application? I have overridden the Closing method to hide my application on the tray; much like what the MSN Messenger does. You hit the X on the system menu and my Closing method sets the e.Cancel to true, sets my WindowState to Minimized and Visibility to false. This all works great; until you attempt a Windows shutdown. My e.Cancel seems to tell the OS that it can't stop. If there is a way for me to determine that the OS is calling my Closing method, then I could leave the e.Cancel alone. Any ideas? Ron Ward
-
Is there a way I can check to see if the OS is getting shut down within my application? I have overridden the Closing method to hide my application on the tray; much like what the MSN Messenger does. You hit the X on the system menu and my Closing method sets the e.Cancel to true, sets my WindowState to Minimized and Visibility to false. This all works great; until you attempt a Windows shutdown. My e.Cancel seems to tell the OS that it can't stop. If there is a way for me to determine that the OS is calling my Closing method, then I could leave the e.Cancel alone. Any ideas? Ron Ward
YeahIGotDotNet posted the code in the VB.NET forum on GotDotNet: http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=40651&Page=1[^] It needs a slight modification, since you're overriding the
OnClosing
method, rather than attaching to the Closing event, and to include the extra cases further down the same discussion:using System.Diagnostics;
...
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);StackTrace t = new StackTrace(false); // NB: If you override, you need frame 6. // If you handle the event, you need frame 7. StackFrame f = t.GetFrame(**6**); switch (f.GetMethod().Name) { case "CallWindowProc": // System menu, or the close button case "DefMDIChildProc": // MDI Child System Menu or close button case "DefFrameProc": // MDI Parent Closing case "ShowDialog": // Modal Dialog Closing case "DispatchMessageW": case "DispatchMessageA": // Task manager case "SendMessage": // Call in code default: //Don't know break; }
}
I would assume that system shutdown would be the same as the task manager option, but you may want to log the method name just to make sure.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Is there a way I can check to see if the OS is getting shut down within my application? I have overridden the Closing method to hide my application on the tray; much like what the MSN Messenger does. You hit the X on the system menu and my Closing method sets the e.Cancel to true, sets my WindowState to Minimized and Visibility to false. This all works great; until you attempt a Windows shutdown. My e.Cancel seems to tell the OS that it can't stop. If there is a way for me to determine that the OS is calling my Closing method, then I could leave the e.Cancel alone. Any ideas? Ron Ward
When the user initiates a shutdown, or when the OS is is about to shutdown, Windows sends out the WM_QUERYENDSESSION to all top level applications. Applications should return a non-zero to allow the the shutdown to continue. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero. You could override the WndProc (Window Procedure) for you main form, and watch for the WM_QUERYENDSESSION message. public enum Msg { WM_QUERYENDSESSION = 0x0011, WM_QUIT = 0x0012, WM_ENDSESSION = 0x0016, } protected override void WndProc(ref Message m) { switch(m.Msg) { case (int)Msg.WM_QUERYENDSESSION: // your code to handle the end session or respond to it break; default: base.WndProc(ref m); break; } } Gaulles http://www.gaulles.com
-
When the user initiates a shutdown, or when the OS is is about to shutdown, Windows sends out the WM_QUERYENDSESSION to all top level applications. Applications should return a non-zero to allow the the shutdown to continue. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero. You could override the WndProc (Window Procedure) for you main form, and watch for the WM_QUERYENDSESSION message. public enum Msg { WM_QUERYENDSESSION = 0x0011, WM_QUIT = 0x0012, WM_ENDSESSION = 0x0016, } protected override void WndProc(ref Message m) { switch(m.Msg) { case (int)Msg.WM_QUERYENDSESSION: // your code to handle the end session or respond to it break; default: base.WndProc(ref m); break; } } Gaulles http://www.gaulles.com