简体   繁体   中英

Unable to restart windows service from my ASP.NET application

I am trying to restart Windows Time Service from my asp.net application using the following code but it always return sa TimeoutException. I have tried various ways to remove this error and restart the service but unfortunately fails in it. The code which i am using for this purpose is shown below:

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 );
    }
}

I am using Windows 7. Can anyone suggest me a solution for this problem? Any help will be appreciated.

Davids comment is of relevance, also you have to check for dependencies to W32time service. There may not exist any? But if it does, it can cause you problem. In case of a 64 bit machine, i would check for the relevance of 'W32..'

[Edit] I attach some code sample that at least works on another Windows 7 computer. The code you provided above works as well, to me.

class Program
{
    static void Main()
    {
        WindowsServiceManager service = new WindowsServiceManager();
        service.Run("W32Time", 2000);
        service.End("W32Time", 2000);
    }
}

public class WindowsServiceManager
{
    internal void Run(string serviceId, int timeOut)
    {
        using (ServiceController serviceController = new ServiceController(serviceId))
        {
            TimeSpan t = TimeSpan.FromMilliseconds(timeOut);
            serviceController.Start();
            serviceController.WaitForStatus(ServiceControllerStatus.Running, t);
        }
    }

    internal void End(string serviceId, int timeOut)
    {
        using (ServiceController serviceController = new ServiceController(serviceId))
        {
            TimeSpan t = TimeSpan.FromMilliseconds(timeOut);
            serviceController.Stop();
            serviceController.WaitForStatus(ServiceControllerStatus.Stopped, t);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM