我希望每一個小時重新啓動Windows服務的C#代碼。我有這個方法 但是當我但它在項目安裝程序或在哪裏?我可以把它放在我想重新啓動它的相同服務中嗎?如何重新啓動windows服務c#
public static void 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);
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}
你想建立一個監視另一項服務的服務?或者您是否期望關閉的服務被關閉以便必須定期手動啓動? – 2013-02-12 16:57:54