Search code examples
c#methodsservice

Looping the if statement to restart a service in case of failure


I have a code where I am checking if the service that I am passing as a parameter in the method is running then stop it & start it with an interval of 10 seconds, basically it's just a restart method. However, is it possible to use a loop with a timer to so that in case after stopping if the service is not starting keep trying to start back up until it is running back again.

The library I am using is System.ServiceProcess

Here's the method I've so far.

public static bool RestartService(string serviceName)
{
    bool result = false;
    using (ServiceController controller = new ServiceController(serviceName))
    {
        try
        {
            if (controller.Status != ServiceControllerStatus.Stopped)
            {
                controller.Stop();
                controller.WaitForStatus(ServiceControllerStatus.Stopped,
                    TimeSpan.FromSeconds(10));
            }

            if (controller.Status != ServiceControllerStatus.Running)
            {
                controller.Start();
                controller.WaitForStatus(ServiceControllerStatus.Running,
                    TimeSpan.FromSeconds(10));
            }

            if (controller.Status == ServiceControllerStatus.Running)
            {
                result = true;
            }
        }
        catch (Exception ex)
        {
            _logger.Error($"RestartService {serviceName} failed, 
                exception_message: {ex.Message}");
        }
    }

    return result;
}

Solution

  • is it possible to use a loop with a timer to so that in case after stopping if the service is not starting keep trying to start back up until it is running back again.

    The method can be called from a Timer (which is probably a good idea so that it runs in the background).

    Converting your code to do retrying until success is as simple as changing if to while:

    public static bool RestartService(string serviceName)
    {
        bool result = false;
        using (ServiceController controller = new ServiceController(serviceName))
        {
            try
            {
                while (controller.Status != ServiceControllerStatus.Stopped)
                {
                    controller.Stop();
                    controller.WaitForStatus(ServiceControllerStatus.Stopped,
                        TimeSpan.FromSeconds(10));
                }
    
                while (controller.Status != ServiceControllerStatus.Running)
                {
                    controller.Start();
                    controller.WaitForStatus(ServiceControllerStatus.Running,
                        TimeSpan.FromSeconds(10));
                }
    
                result = true;
            }
            catch (Exception ex)
            {
                _logger.Error($"RestartService {serviceName} failed, 
                    exception_message: {ex.Message}");
            }
        }
    
        return result;
    }