[C#/COMMON] ServiceController 클래스 : 윈도우즈 서비스 재시작하기

■ ServiceController 클래스 : 윈도우즈 서비스 재시작하기
————————————————————————————————————————
using System;
using System.ServiceProcess;

#region 서비스 재시작하기 – RestartService(serviceName, timeOut)

/// <summary>
/// 서비스 재시작하기
/// </summary>
/// <param name="serviceName">서비스명</param>
/// <param name="timeOut">타임아웃 (밀리초)</param>
public void RestartService(string serviceName, int timeOut)
{
ServiceController serviceController = new ServiceController(serviceName);

try
{
int tickCount1 = Environment.TickCount;

TimeSpan timeout = TimeSpan.FromMilliseconds(timeOut);

serviceController.Stop();

serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

int tickCount2 = Environment.TickCount;

timeout = TimeSpan.FromMilliseconds(timeOut – (tickCount2 – tickCount1));

serviceController.Start();

serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
}
}

#endregion
————————————————————————————————————————

Advertisements