【发布时间】:2014-10-15 08:07:18
【问题描述】:
我正在使用以下函数来停止我的 Windows 服务(超时):
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;
}
}
我检查服务的状态。如果不是ServiceControllerStatus.Stopped 我调用这个函数。
我已经测试过,当服务器的状态为 Running 或 Stopped 时程序可以工作。但是我想知道当状态是其他状态时会发生什么,例如 StopPending、StartPending 等。如果我告诉我的服务在状态是其中之一时停止以上,Stop 命令还能发挥作用吗?
【问题讨论】:
标签: c# windows-services servicecontroller