我想從我的asp.net應用程序使用下面的代碼重新啓動Windows時間服務,但它總是返回一個TimeoutException。我嘗試過各種方法來刪除這個錯誤並重新啓動服務,但不幸的是它失敗了。我用於此目的代碼如下所示:無法從我的ASP.NET應用程序重新啓動Windows服務
private ServiceController service = new ServiceController("W32Time", Environment.MachineName);
private TimeSpan timeout = TimeSpan.FromMilliseconds(35000);//15000 was the old value
// Restart W32Time Service
private void RestartService()
{
try
{
// Stop service if it is running
if(service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
// Start service if it is stopped
if(service.Status == ServiceControllerStatus.Stopped)
{
if (!(service.Status.Equals(ServiceControllerStatus.Stopped) || service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Stop();
}
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch(Exception ex)
{
log.Error("Error in restarting windows service.", ex);
}
}
我正在使用Windows 7.任何人都可以爲我提供這個問題的解決方案嗎?任何幫助將不勝感激。
應用程序運行的帳戶是否具有訪問服務的權限? – Davidann 2011-04-13 15:37:32