Developping for the translation industry RSS 2.0

 Wednesday, December 17, 2008

This method tries to stop the service (and waits until it's stopped) then it begins to start the service (and waits until the service is running). The specified timeout is used for both operations together.

First, you need to add a using directive to System.ServiceProcess.

using System.ServiceProcess;

This is the method to restart the service:

public static void RestartService(string serviceName, int timeoutMilliseconds)
    ServiceController service =
new ServiceController(serviceName);
        int millisec1 = Environment.TickCount;
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

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

Wednesday, December 17, 2008 3:22:24 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
C# | Code Snippet

About the author/Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2019
Stanislas Biron
Sign In
Total Posts: 135
This Year: 0
This Month: 0
This Week: 0
Comments: 1
All Content © 2019, Stanislas Biron