Cannot restart a Service

此生再无相见时 提交于 2019-12-18 09:53:31

问题


I have this code to restart a service, but this is not working.

I can start and stop individually but not restart which involves me to first stop and start the service.

try
{
    //service.Stop();
    //service.Start();
    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
{
    // ...
}

It is simply going in the catch section.

I don't know where i am going wrong.

Any suggestions.??

UPDATE:

So I took the idea from the correct answer below:

This is what need to be done>

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
    ServiceController service = new ServiceController(serviceName);

    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    if (!(service.Status.Equals(ServiceControllerStatus.Stopped) || service.Status.Equals(ServiceControllerStatus.StopPending)))
    {
        service.Stop();
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    }
    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

    if (!(service.Status.Equals(ServiceControllerStatus.Running) || service.Status.Equals(ServiceControllerStatus.StartPending)))
    {
        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    }
}

回答1:


Having an empty catch block catching all exceptions is rarely a good idea as serious problems easily slip through.

Modify your code to at least do some logging in the catch block, e.g.

catch (Exception ex)
{
    System.Diagnostics.Trace.WriteLine(ex.ToString());
}

You can then either attach a trace listener in the constructor of your service or use the DebugView tool from Sysinternals to read the trace message. If you want to take this a step further you might want to include a logging library like log4net into your project.

My guess would be that your are getting a TimeoutException because stopping the service takes longer as you expected. Have you tried increasing the timeout or waiting infinitely by removing the timeout parameter?

Update:

You probably need to check whether your service is started or not:

if  (!(service.Status.Equals(ServiceControllerStatus.Stopped) 
       || service.Status.Equals(ServiceControllerStatus.StopPending)))
{
    service.Stop();
}
service.Start();


来源:https://stackoverflow.com/questions/3309990/cannot-restart-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!