【发布时间】:2011-05-04 09:45:27
【问题描述】:
我正在尝试使用以下代码从我的 asp.net 应用程序重新启动 Windows 时间服务,但它总是返回一个 TimeoutException。我尝试了各种方法来消除此错误并重新启动服务,但不幸的是它失败了。我为此目的使用的代码如下所示:
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。谁能建议我解决这个问题?任何帮助将不胜感激。
【问题讨论】:
-
运行应用的账号是否有权限访问服务?