让你的GUI程序随WINDOWS服务一起启动
2009-05-14 08:28:31 来源:WEB开发网3.2 检查并启动Windows服务
/// <summary>
/// 启动已暂停或停止的服务
/// </summary>
private void StartService()
{
try
{
foreach (string serviceName in rwCnfg.GsServiceNames)
{
ServiceController myService = new ServiceController(serviceName);
ServiceControllerStatus status = myService.Status;
switch (status)
{
case ServiceControllerStatus.ContinuePending:
break;
case ServiceControllerStatus.PausePending:
break;
case ServiceControllerStatus.StartPending:
break;
case ServiceControllerStatus.Running:
break;
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.Stopped:
{
myService.Start();
myService.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 2, 0));
Common.wLog("完成启动服务: " + myService.ServiceName + " . " + System.DateTime.Now.ToString());
}
break;
case ServiceControllerStatus.StopPending:
break;
default:
break;
}
}
}
catch (Exception err)
{
Common.wLog(err.ToString());
}
}
更多精彩
赞助商链接