I have written a windows service in c# that process a lot data. when we stop it try for sometime 20/30 seconds and then throws exception.
I want to implement ServiceBase.RequestAdditionalTime() in OnStop event.
I want to know the exact timeout after which windows service throws the exception, so that I can request additional time just before it.
I searched but did not find this default stop timeout value.
To increase the timeout value in the registry, follow these steps: Start Registry Editor (Regedit.exe). To change the value data for the ServicesPipeTimeout DWORD value to 60000 in the Control key, follow these steps: Locate and then click the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet.
The easiest way to stop a stuck service is to use the built-in taskkill command-line tool. First of all, you need to find the PID (process identifier) of the service. As an example, let's take the Windows Update service. Its system name is wuauserv (you can check the name in the service properties in the services.
I wrote the following code to achieve it.
protected override void OnStop()
{
int timeout = 10000;
var task = Task.Factory.StartNew(() => MyTask());
while (!task.Wait(timeout))
{
RequestAdditionalTime(timeout);
}
}
The above code starts a Task in Parallel to the main thread (Task start running immediately), next line is to check if task is completed or not every 10 seconds and if it is not completed it requests additional 10 seconds and keep checking till task get completed.
Though a number of people have mentioned the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout
registry key, according to this "Service Control Handler" article from Microsoft that registry entry only controls the max amount of time a service can take to shut down when Windows itself is being shut down or restarted:
<...> 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 <...>
If Windows is not in the process of restarting or shutting down, then the default amount of time Windows will wait for a service to shut down is 30 seconds. However, applications can make requests for additional time, which will be honored up to 125 seconds total (summed across all requests).
On Windows Server 2003 and later, this default timeout can be changed via the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServicesPipeTimeout
registry key, as described in this Microsoft support article (and this ServerFault question). It's not clear if this applies to Windows 7/8/10, as the article only mentions server versions.
If a restart/shutdown has been initiated on the machine, the WaitToKillServiceTimeout
registry key value (if present) specifies the maximum amount of time Windows will allow the application will be allowed, overriding the OS default.
Presumably this is so that applications cannot arbitrarily delay shutdown beyond the default (or what the administrator specified via the WaitToKillServiceTimeout
registry entry).
It's set in the registry on subkey:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
with string value WaitToKillServiceTimeout
. If not defined, it defaults to 20000 (ms). On my machine it seems to be set to 12000 (12s). I have never touched it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With