Developping for the translation industry RSS 2.0



 Wednesday, 17 December 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);
    try
    {
        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
    {
        // ...
    }
}

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

Navigation
Advertisement
About the author/Disclaimer

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

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