I have a windows service that imports files into a database. When the Service receives a notice of termination, it should, if possible, complete the current Import. This may take several minutes.
Therefore, I use the ServiceBase.RequestAdditionalTime method for the SCM signal that the Service is still up and running.
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."); }
During testing, I can not find any other behavior of the Service using the RequestAdditionalTime method. If I remove the RequestAdditionalTime call, the Service behaves the same:
- In the Stop service, SCM waits about 2 minutes and reports that the service is not responding (error 1053). After that, the state of the service remains
Stopping until my worker shuts down. - When shutting down, the system does not wait for extra time. It seems to kill all services after 5 seconds (
WaitToKillServiceTimeout registry WaitToKillServiceTimeout )
Question
What is the impact of RequestAdditionalTime ? Where should I see the difference when using it and how can I check it? Several times I read that SCM will kill the Service if it does not require additional time. But I did not see this behavior.
All tests performed on my local development machine (Win 8.1), provided that the behavior is the same as in Windows Server.
source share