Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is Windows Shutting Down

Is Windows Shutting Down

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OBRon
    wrote on last edited by
    #1

    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

    R G 2 Replies Last reply
    0
    • O OBRon

      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

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • O OBRon

        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

        G Offline
        G Offline
        Gaul
        wrote on last edited by
        #3

        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

        O 1 Reply Last reply
        0
        • G Gaul

          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

          O Offline
          O Offline
          OBRon
          wrote on last edited by
          #4

          Checking for the WM_QUERYENDSESSION and WM_ENDSESSION worked perfectly! Thanks Gaul.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups