0

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);
    }
}
gagan mahatma
  • 336
  • 2
  • 9
user175084
  • 4,550
  • 28
  • 114
  • 169
  • 4
    change `catch` to `catch (Exception ex)` and save the exception to a log file or the Windows Event Log. Then please post the text of that exception and we'll be able to troubleshoot what's actually going wrong. As it stands, the only advice someone can give is "an exception is occurring." – Jesse C. Slicer Jul 22 '10 at 14:38
  • 2
    Perhaps you should read the exception, which is raised, and not suppress it. Might be a help. ;o) – DHN Jul 22 '10 at 14:38
  • What exception are you seeing? You should catch some exception just so you can get and log a message. – David Hoerster Jul 22 '10 at 14:38
  • what exception are you getting in the catch? – Pharabus Jul 22 '10 at 14:38
  • so the server.Stop is not working. i get the error :"The service has not been started" in stack trace it shows: "[Win32Exception (0x80004005): The service has not been started] [InvalidOperationException: Cannot stop XYZService service on computer '.'.] System.ServiceProcess.ServiceController.Stop() +410" – user175084 Jul 22 '10 at 15:00

1 Answers1

5

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();
artfulhacker
  • 4,823
  • 1
  • 37
  • 31
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • so the server.Stop is not working. i get the error :"The service has not been started" in stack trace it shows: "[Win32Exception (0x80004005): The service has not been started] [InvalidOperationException: Cannot stop XYZService service on computer '.'.] System.ServiceProcess.ServiceController.Stop() +410" – user175084 Jul 22 '10 at 15:00
  • so what is happenin in your above code is that if the condition is true it is stopping the service and doing the postback so th does not execute the service.Start(). so the service is stopping but not restarting. – user175084 Jul 22 '10 at 15:21
  • @user175084: What do you mean with "postback"? The sample code above would stop the service if it is *not* in the *Stopped* or *StopPending* state. After that it will start the service. – Dirk Vollmar Jul 22 '10 at 15:41
  • so when the service is already started and you press restart... i get the error again saying The service has not been started] [InvalidOperationException: Cannot START XYZService service on computer '.'. – user175084 Jul 22 '10 at 15:43
  • @user175084: Did you query the service controller for its current status? What does it tell you? – Dirk Vollmar Jul 22 '10 at 16:21
  • so ur method for checking was correct.. we have to now check for if the service is started and give it a time out... – user175084 Jul 22 '10 at 16:38