次の関数を使用して、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 コマンドは引き続き機能しますか?