0
我需要把一些代碼來重新啓動我的Windows服務時面臨的一個嚴重錯誤(重大事件)Windows服務 - 重新啓動?
我有這個功能
public Boolean RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
int millisec2 = Environment.TickCount;
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
if (service.Status != ServiceControllerStatus.Running)
{
return false;
}
}
catch (System.ServiceProcess.TimeoutException ex)
{
return false;
}
return true;
}
的事情是,這顯然不是一件好事,因爲雖然它停止...它不能再次開始:)
有沒有辦法重新構建它可以「記住」啓動自己?
Thanx!
謝謝Thorsten!但是我應該在哪裏放置Environment.Exit(1);碼? – PanosPlat
那麼,在你的代碼的什麼時候你打算調用你的非功能'RestartService'方法?那裏! :-)你也可以用'Environment.Exit(1)'替換'RestartService'方法的內容,並從方法聲明中刪除參數。如果您沒有發現允許您重新啓動服務的異常,它仍然可以正常工作,因爲在這種情況下服務會意外停止,系統也會因爲第一個配置步驟而重新啓動服務。 –