5

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Thomas Zweifel
  • 627
  • 1
  • 6
  • 19
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 03 '15 at 06:46
  • Where do you have the logic of importing files into DB ? why can't you move the service stopping code to end of the import file method ? so that it would make sure service stops once the import completes ? – Kurubaran Jun 03 '15 at 07:00
  • @Kurubaran The logic is in the worker. The Worker's stop method cancels all pending imports in the queue and waits for the current Import to complete. Therefor the Service will stop gracefully when the current Import has finished. – Thomas Zweifel Jun 03 '15 at 09:01
  • I can't confirm, but [this post](http://stackoverflow.com/a/2011335/634824) seems to suggest that the additional time requested needs to be under 2 minutes. Can you try that and see if it makes a difference? – Matt Johnson-Pint Jun 11 '15 at 22:05

0 Answers0