Search code examples
c#.netwindows-services.net-4.6servicecontroller

Does ServiceController.WaitForStatus set the status or just wait?


I'm using ServiceController.WaitForStatus for the first time to limit time wasted on trying to start services that won't start. My assumption is to use it like this:

var sc = new ServiceController(_monitoredService);
var seconds = _startWaitTimeout / 1000;
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 0, seconds));
if (sc.Status != ServiceControllerStatus.Running)
{
    _logger.Warn($"'{_monitoredService}' did not start within {seconds} seconds.");
}

Then a little devil on my shoulder suggested that WaitForStatus might take it upon itself to attempt to set the status before waiting. Does it?


Solution

  • No.

    From MSDN Documentations: Use WaitForStatus to suspend an application's processing until the service has reached the required status.

    Thats all, it only suspends the calling thread and polls the status - as mentioned in the Documentation - every some 250ms until either Service State or Timeout is reached.