2

I have created a service, where I am sending commands to the service via MSMQ.

Now I need to restart the service but when it reaches to the stop command it just throws error or it is not able to read the next line as the service performing this task stops.

The code I am using is :

ServiceController service = new ServiceController(serviceName);
            try
            {
                int millisec1 = Environment.TickCount;
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                ShutdownService(serviceName, timeoutMilliseconds);

                // count the rest of the timeout
                int millisec2 = Environment.TickCount;
                timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch (Exception ex)
            {
                s_log.Error(ex);
            }

How can I restart service by itself as logically when the service will stop, it will not execute next line of code.

Moiz
  • 2,409
  • 5
  • 27
  • 50
  • Please check out this link http://www.csharp-examples.net/restart-windows-service/ – Vimal CK Feb 10 '15 at 12:30
  • I have picked this code from this link only. but when I am trying to stop the service, it is disposed and again when I try to access the service object, it throws error – Moiz Feb 10 '15 at 12:31
  • 1
    Why you need to *restart* the service ? Guessing you are doing something wrong. – Kurubaran Feb 10 '15 at 12:33
  • Its based on some of my parameters in my system which effects my service. so when the parameters are changed, I need my service to take those effects. – Moiz Feb 10 '15 at 12:44

1 Answers1

4

Right click on the service and select its properties, then choose recovery tab. Set the service to restart after failure on 1st failure. Then to restart the service, just call Environment.Exit(1) and Windows will restart the service.

Kurubaran
  • 8,696
  • 5
  • 43
  • 65
  • 2
    My take is that OP should try approaching the problem from a different direction. Rather than continually shutting down and restarting your service how about allowing the service to run continually but give it a System.Timers.Timer _timer member. You can set the _timer.Interval to Environment.TickCount and add an ElapsedEventHandler to _timer.Elapsed. Then you can do whatever you want to do in the ElapsedEventHandler at the frequency you set in Environment.TickCount without trying to get your service to die and resurrect like a mad zombie! – jksemple Feb 10 '15 at 12:41
  • No I have one option in my web application, which will trigger a msg to Service and it will then pick the msg from MSMQ and will initialize the restart. what happens is it stops the service but then it does nothing – Moiz Feb 10 '15 at 12:54
  • 1
    First, you should apply the principle of 'separation of concerns'. A service should not restart itself. I would suggest that the another service reads the msmq and uses ServiceManager class to restart the service in question. –  Feb 10 '15 at 13:01
  • so is it that -once the service is marked as stopped will not read any code written inside the service ? – Moiz Feb 10 '15 at 13:04
  • It will try a graceful stop otherwise on timeout it will forcefully kill the service process. I think your approach trying to solve the underlying issue may not be the correct one. –  Feb 10 '15 at 13:12
  • okay so what does you suggest to create one more service which will handle this service start/stop – Moiz Feb 10 '15 at 13:13
  • 1
    Well, the question is why you want to restart a service in the first place, unless it is for deploying new code. But if I have to I will have a separate service to perform the restart. –  Feb 10 '15 at 13:18