I have a Windows Service that Imports files into DB. When the Service receives to stop notification, It should complete the current Import if possible. It can take several minutes to complete.
Therefore I use the ServiceBase.RequestAdditionalTime
method to Signal the SCM that the Service is still working and operable.
protected override void OnStop()
{
var waitTime = TimeSpan.FromSeconds(5);
var additionalWaitTime = TimeSpan.FromMinutes(3);
Trace.Write("Stopping service...");
var task = Task.Factory.StartNew(() => worker.Stop());
while (!task.Wait(waitTime))
{
Trace.Write("Requesting additional time...");
RequestAdditionalTime((int)additionalWaitTime.TotalMilliseconds);
Trace.Write("Waiting for {1} to complete...");
}
Trace.Write("Service stopped.");
}
While testing, I cannot find any different behaviour of the Service using the RequestAdditionalTime
method. If I remove the RequestAdditionalTime
call, the Service behaves the same way:
- On Service Stop, the SCM waits for about 2 minutes and reports the Service is not responding (Error 1053). After that, the Service state remains
Stopping
until my worker completes. - On Shutdown, the System does not wait for the additional time. It seems to kill all Services after 5 seconds (Registry Key
WaitToKillServiceTimeout
)
Question
What effect does the RequestAdditionalTime
have? Where am I supposed to see a difference when using it and how can i test it?
Several times i read that the SCM will kill the Service if no additional time requested. But I couldn't see that behaviour.
All tests where run on my local development machine (Win 8.1), assuming that the behaviour is the same as it will be on a Windows Server OS.