I am using the following function to stop my windows service (with a timeout):
public int StopService()
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
return 1;
}
catch (Exception ex)
{
logger.Log(LoggingLevel.Error,
this.GetType().Name + ":" + MethodBase.GetCurrentMethod().Name,
string.Format("{0}", ex.Message));
return -1;
}
}
I check the status of the service. And if it is not ServiceControllerStatus.Stopped
I call this function.
I have tested and the program works when the status of the server is Running or Stopped. But I would like to know what happens when the status is other states such as StopPending, StartPending, etc. If I tell my service to stop while the state is one of the above, will the Stop command still do its job?