what is the maximum time windows service wait to process stop request and how to request for additional time

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.

Leave a Comment