none
Windows Service OnStop() wait for finished processing RRS feed

  • Question

  • Hi,

    I'm creating a Windows Service, using C#, that periodically do some operations on a SQL Server db. Every 5 min, it does OPERATION1, every 60 min it does OPERATION2, and so on. I want something like that:

    if the service is stopped but OPERATION1 or OPERATION2 aren't complete, in OnStop() method there should be something that delay the stop signal untill OPERATION1 or OPERATION2 end.

    How can I do that?

    Thursday, October 10, 2019 7:37 AM

Answers

  • blocking in the OnStop handler is the wrong way to do it: this will be reported as an error after 30 seconds.
    A .NET service should call ServiceBase.RequestAdditionalTime when it needs more time.
    But there are some timeouts / behaviour when shutting down the OS:
    Service Control Handler Function

    However, to prevent a service from stopping shutdown, there is a limit to how long the service controller waits. If the service is being shut down through the Services snap-in, the limit is 125 seconds. If the operating system is rebooting, the time limit is specified in the WaitToKillServiceTimeout value of the following registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control

    Thursday, October 10, 2019 5:03 PM

All replies

  • The OnStop() could check a flag set by by the process call it Activeflg that is set by the process. If the flag is true, you should go into a while loop making the Onstop() wait until the flag is false set by the process when it is complete.
    Thursday, October 10, 2019 8:33 AM
  • blocking in the OnStop handler is the wrong way to do it: this will be reported as an error after 30 seconds.
    A .NET service should call ServiceBase.RequestAdditionalTime when it needs more time.
    But there are some timeouts / behaviour when shutting down the OS:
    Service Control Handler Function

    However, to prevent a service from stopping shutdown, there is a limit to how long the service controller waits. If the service is being shut down through the Services snap-in, the limit is 125 seconds. If the operating system is rebooting, the time limit is specified in the WaitToKillServiceTimeout value of the following registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control

    Thursday, October 10, 2019 5:03 PM
  • Hi rsslldblsnglll, 

    Thank you for posting here.

    According to your description, I find two related references.

    1. Windows service OnStop wait for finished processing
    2. Waiting for a timer elapsed event to complete before application/service closes/stops

    Hope them can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 11, 2019 6:21 AM
    Moderator
  • blocking in the OnStop handler is the wrong way to do it: this will be reported as an error after 30 seconds.
    A .NET service should call ServiceBase.RequestAdditionalTime when it needs more time.
    But there are some timeouts / behaviour when shutting down the OS:
    Service Control Handler Function

    However, to prevent a service from stopping shutdown, there is a limit to how long the service controller waits. If the service is being shut down through the Services snap-in, the limit is 125 seconds. If the operating system is rebooting, the time limit is specified in the WaitToKillServiceTimeout value of the following registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control

    Thank you a lot :)
    Monday, October 14, 2019 2:11 PM