locked
abort shutdown? RRS feed

  • Question

  • Hi,

    Is there a way in which a windows service can abort a system shutdown? In a desktop application, I can handle the SessionEnding event and abort a shutdown. Can a windows service do something similar? I can register for and handle the shutdown event, but if the service is doing something critical (which takes time) and should not be shutdown, how does it prevent the abort the system shutdown initiated by a user?

    Any pointers would be very helpful.

    Thanks,
    Roshan

    Monday, June 27, 2005 4:23 AM

Answers

  •  broshan wrote:
    Is there a way in which a windows service can abort a system shutdown?
    That's not a very polite thing to do.  Well behaved services should shut down when they are asked to.  Imagine how frustrating it would be to try shutting down a system only to have something cancel the shutdown every time you requested one.
    In a desktop application, I can handle the SessionEnding event and abort a shutdown.
    You can ask, but note that setting cancel to false does not guarantee the session will not end.
    if the service is doing something critical (which takes time) and should not be shutdown, how does it prevent the abort the system shutdown initiated by a user?
    A service isn't told the system is shutting down, it just gets told to stop.  The service doesn't know if it is stopping because of a shutdown or stopping because someone issued a stop service command.  Never mind that bit, I don't know what I was thinking

    If a service doesn't stop in a timely fashion (for stop or for shutdown), the Service Control Manager will kill the service (process terminate) on the assumption that it is broken.

    If a service has to take a long time to stop, the well behaved way to do that in unmanaged code is to call SetServiceStatus on a regular basis incrementing the dwCheckPoint parameter to let the SCM know the service is still working on stopping and still making progress towards actually stopping. 

    I don't think a service can do this indefinitely though.  I've never written an actual test but I believe eventually the SCM is going to give up and just shoot the service anyway.

    For services implemented in managed code I don't think there is any way to maniipulate status and the CheckPoint while shutting down.  Off hand I don't think you could even use P/Invoke to call SetServiceStatus because you can't get hold of the service handle.
    Monday, June 27, 2005 5:56 AM

All replies

  • Hi,

    You can use ServiceBase.CanShutdown property of the Service class to true which would result in calling ServiceBase.OnShutDown method when the System shuts down.You can override this method to write your own code to abort shutdown or whetever.I wonder how would you abort the shutdown..could you share more info on that...

    Thanks,
    Suresh.
    Monday, June 27, 2005 5:52 AM
  •  broshan wrote:
    Is there a way in which a windows service can abort a system shutdown?
    That's not a very polite thing to do.  Well behaved services should shut down when they are asked to.  Imagine how frustrating it would be to try shutting down a system only to have something cancel the shutdown every time you requested one.
    In a desktop application, I can handle the SessionEnding event and abort a shutdown.
    You can ask, but note that setting cancel to false does not guarantee the session will not end.
    if the service is doing something critical (which takes time) and should not be shutdown, how does it prevent the abort the system shutdown initiated by a user?
    A service isn't told the system is shutting down, it just gets told to stop.  The service doesn't know if it is stopping because of a shutdown or stopping because someone issued a stop service command.  Never mind that bit, I don't know what I was thinking

    If a service doesn't stop in a timely fashion (for stop or for shutdown), the Service Control Manager will kill the service (process terminate) on the assumption that it is broken.

    If a service has to take a long time to stop, the well behaved way to do that in unmanaged code is to call SetServiceStatus on a regular basis incrementing the dwCheckPoint parameter to let the SCM know the service is still working on stopping and still making progress towards actually stopping. 

    I don't think a service can do this indefinitely though.  I've never written an actual test but I believe eventually the SCM is going to give up and just shoot the service anyway.

    For services implemented in managed code I don't think there is any way to maniipulate status and the CheckPoint while shutting down.  Off hand I don't think you could even use P/Invoke to call SetServiceStatus because you can't get hold of the service handle.
    Monday, June 27, 2005 5:56 AM
  • Hi Suresh,

    Yes I can use ServiceBase.CanShutdown to register for and handle shutdown events. What I want to know is, if it is possible to programatically abort the shutdown in the event handler? Basically, if the service is doing something critical and some one (by mistake) tries to shutdown the system. In such a scenario, if I can abort the system shutdown (not postpone/delay it) it would be great !!!

    Thanks,
    Roshan
    Monday, June 27, 2005 6:11 AM
  •  Frank Boyne wrote:
    Thats' not a very polite thing to do.  Well behaved services should shut down when they are asked to.  Imagine how frustrating it would be to try shutting down a system only to have something cancel the shutdown every time you requested one


    Aborting shutdown is not something I want to do always. My requirement is to abort shutdown only when some critical task is being done by the service. The user is always free to stop the service and shutdown. Is there a method, wherein, I can atleast try to abort the shutdown ?

    Thanks,
    Roshan
    Monday, June 27, 2005 6:24 AM