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. Detect system shutdown

Detect system shutdown

Scheduled Pinned Locked Moved C#
windows-admincsharpdotnetsysadminhelp
7 Posts 5 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.
  • B Offline
    B Offline
    Bob Stanneveld
    wrote on last edited by
    #1

    Hello all, I've developed a monitoring service which monitors some services and starts them when they are not running. I found a problem when the server machine is shutting down and / or restarting. Windows is shutting down some services which are monitored before my monitoring service is shut down. Result is that my monitoring service detects that the service is not running and then starts the service. Windows continues to shut down and registry corruption is the result. I thought that detecting whether the OS (Windows Server 2003) is shutting down would be a nice solution. However this is not an easy task. The OnShutdown() method is not called in my derived class. Furthermore I don't know when this method is invoked. Is it when the shutdown is initiated before the services are stopped, or is it invoked when the OS is stopping the services? Is there a simple way to detect whether the OS is shutting down or restarting without responding to Windows messages / events? I'm using the .NET framework 2.0 Thanks in advance, Bob Stanneveld

    Behind every great black man...             ... is the police. - Conspiracy brother

    O L D J 4 Replies Last reply
    0
    • B Bob Stanneveld

      Hello all, I've developed a monitoring service which monitors some services and starts them when they are not running. I found a problem when the server machine is shutting down and / or restarting. Windows is shutting down some services which are monitored before my monitoring service is shut down. Result is that my monitoring service detects that the service is not running and then starts the service. Windows continues to shut down and registry corruption is the result. I thought that detecting whether the OS (Windows Server 2003) is shutting down would be a nice solution. However this is not an easy task. The OnShutdown() method is not called in my derived class. Furthermore I don't know when this method is invoked. Is it when the shutdown is initiated before the services are stopped, or is it invoked when the OS is stopping the services? Is there a simple way to detect whether the OS is shutting down or restarting without responding to Windows messages / events? I'm using the .NET framework 2.0 Thanks in advance, Bob Stanneveld

      Behind every great black man...             ... is the police. - Conspiracy brother

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      http://www.google.co.uk/search?hl=en&q=c%23+detect+shutdown&meta=[^]

      1 Reply Last reply
      0
      • B Bob Stanneveld

        Hello all, I've developed a monitoring service which monitors some services and starts them when they are not running. I found a problem when the server machine is shutting down and / or restarting. Windows is shutting down some services which are monitored before my monitoring service is shut down. Result is that my monitoring service detects that the service is not running and then starts the service. Windows continues to shut down and registry corruption is the result. I thought that detecting whether the OS (Windows Server 2003) is shutting down would be a nice solution. However this is not an easy task. The OnShutdown() method is not called in my derived class. Furthermore I don't know when this method is invoked. Is it when the shutdown is initiated before the services are stopped, or is it invoked when the OS is stopping the services? Is there a simple way to detect whether the OS is shutting down or restarting without responding to Windows messages / events? I'm using the .NET framework 2.0 Thanks in advance, Bob Stanneveld

        Behind every great black man...             ... is the police. - Conspiracy brother

        L Offline
        L Offline
        lucjon
        wrote on last edited by
        #3

        I searched the web and you can use the Microsoft.Win32.SessionEnded event, as described here. Also, the Shutdown event in ApplicationEvents.vb or similar (I don't know about C#/++ or J#) refers to the Shutdown of your application, not the system.

        B 1 Reply Last reply
        0
        • L lucjon

          I searched the web and you can use the Microsoft.Win32.SessionEnded event, as described here. Also, the Shutdown event in ApplicationEvents.vb or similar (I don't know about C#/++ or J#) refers to the Shutdown of your application, not the system.

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          Hello, Thanks for the reaction! Unfortunately this is no help since my application is not receiving any messages. So the SessionEnded event will never fire for my service.

          Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          1 Reply Last reply
          0
          • B Bob Stanneveld

            Hello all, I've developed a monitoring service which monitors some services and starts them when they are not running. I found a problem when the server machine is shutting down and / or restarting. Windows is shutting down some services which are monitored before my monitoring service is shut down. Result is that my monitoring service detects that the service is not running and then starts the service. Windows continues to shut down and registry corruption is the result. I thought that detecting whether the OS (Windows Server 2003) is shutting down would be a nice solution. However this is not an easy task. The OnShutdown() method is not called in my derived class. Furthermore I don't know when this method is invoked. Is it when the shutdown is initiated before the services are stopped, or is it invoked when the OS is stopping the services? Is there a simple way to detect whether the OS is shutting down or restarting without responding to Windows messages / events? I'm using the .NET framework 2.0 Thanks in advance, Bob Stanneveld

            Behind every great black man...             ... is the police. - Conspiracy brother

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            You can call the Win32 API function RegisterServiceCtrlHandlerEx[^] to register your service to receive window message-based notification, like WM_QUERYENDSESSION or WM_ENDSESSION. But, there's a downside to doing this. Once you call RegisterServiceCtrlHandlerEx, the normal ServiceBase's control handlers, like OnStart, OnStop, ..., will no longer work. You'll have to handle the Service Control messages and do the dispatching to your handlers yourself. Sorry, I don't have any examples handy.

            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            1 Reply Last reply
            0
            • B Bob Stanneveld

              Hello all, I've developed a monitoring service which monitors some services and starts them when they are not running. I found a problem when the server machine is shutting down and / or restarting. Windows is shutting down some services which are monitored before my monitoring service is shut down. Result is that my monitoring service detects that the service is not running and then starts the service. Windows continues to shut down and registry corruption is the result. I thought that detecting whether the OS (Windows Server 2003) is shutting down would be a nice solution. However this is not an easy task. The OnShutdown() method is not called in my derived class. Furthermore I don't know when this method is invoked. Is it when the shutdown is initiated before the services are stopped, or is it invoked when the OS is stopping the services? Is there a simple way to detect whether the OS is shutting down or restarting without responding to Windows messages / events? I'm using the .NET framework 2.0 Thanks in advance, Bob Stanneveld

              Behind every great black man...             ... is the police. - Conspiracy brother

              J Offline
              J Offline
              Jupiter9
              wrote on last edited by
              #6

              One trick is to make the services dependent on the monitoring service. Then on the service OnStart, set the shutdown priority for the monitoring service to a high priority to guarantee everything happens in the correct sequence. This can be done via: NOTE: the SHUTDOWN_LEVEL is set at the minimum user-level available priority to set the service at the top most spot of the list. using System.Runtime.InteropServices; [DllImport("Kernel32.DLL")] private static extern bool SetProcessShutdownParameters(Int32 dwLevel, Int32 dwFlags); private const Int32 SHUTDOWN_NORETRY = 0x00000001; private const Int32 SHUTDOWN_LEVEL = 0x281; protected override void OnStart(string[] args) { if (!SetProcessShutdownParameters(SHUTDOWN_LEVEL, SHUTDOWN_NORETRY)) System.Diagnostics.Debug("Failed to set shutdown priority."); } However, if the service monitor is simply acting as a recovery app, then using the service recovery options may be more optimal to allow windows service manager to automatically restart the service for you.

              B 1 Reply Last reply
              0
              • J Jupiter9

                One trick is to make the services dependent on the monitoring service. Then on the service OnStart, set the shutdown priority for the monitoring service to a high priority to guarantee everything happens in the correct sequence. This can be done via: NOTE: the SHUTDOWN_LEVEL is set at the minimum user-level available priority to set the service at the top most spot of the list. using System.Runtime.InteropServices; [DllImport("Kernel32.DLL")] private static extern bool SetProcessShutdownParameters(Int32 dwLevel, Int32 dwFlags); private const Int32 SHUTDOWN_NORETRY = 0x00000001; private const Int32 SHUTDOWN_LEVEL = 0x281; protected override void OnStart(string[] args) { if (!SetProcessShutdownParameters(SHUTDOWN_LEVEL, SHUTDOWN_NORETRY)) System.Diagnostics.Debug("Failed to set shutdown priority."); } However, if the service monitor is simply acting as a recovery app, then using the service recovery options may be more optimal to allow windows service manager to automatically restart the service for you.

                B Offline
                B Offline
                Bob Stanneveld
                wrote on last edited by
                #7

                Thanks for the information! I'll try this to solve the problem. I have just one question. http://msdn2.microsoft.com/en-us/library/ms686227.aspx[^] says the following about the first parameter: [in] The shutdown priority for a process relative to other processes in the system. The system shuts down processes from high dwLevel values to low. The highest and lowest shutdown priorities are reserved for system components. This parameter must be in the following range of values. The value 3FF is listed as the highest value for applications in the table following the description. Shouldn't SHUTDOWN_LEVEL be equal to 3FF? Unfortunatly my monitoring service is doing more than just restarting the other services so the recovery options are not sufficient in this case.

                Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                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