[开发笔记]-控制Windows Service服务运行

时间:2022-04-03 06:00:43

用代码实现动态控制Service服务运行状态。

效果图:

[开发笔记]-控制Windows Service服务运行

代码:

        #region 启动服务
/// <summary>
/// 启动服务
/// </summary>
/// <param name="scname"></param>
void XServiceStart(string scname)
{
ServiceController sc = new ServiceController(scname);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.StopPending:
case ServiceControllerStatus.Stopped:
sc.Start();//启动服务
//等待服务达到指定状态
sc.WaitForStatus(ServiceControllerStatus.Running);
break;
default:
break;
} }
#endregion #region 停止服务
/// <summary>
/// 停止服务
/// </summary>
/// <param name="scname"></param>
void XServiceStop(string scname)
{
ServiceController sc = new ServiceController(scname);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Running:
case ServiceControllerStatus.StartPending:
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.PausePending:
case ServiceControllerStatus.ContinuePending:
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
break;
default: break;
}
}
#endregion #region 暂停服务
/// <summary>
/// 暂停服务
/// </summary>
/// <param name="scname"></param>
void XServicePause(string scname)
{
ServiceController sc = new ServiceController(scname);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Running:
case ServiceControllerStatus.StartPending:
sc.Pause();
sc.WaitForStatus(ServiceControllerStatus.Paused);
break;
default: break;
}
}
#endregion #region 继续服务
/// <summary>
/// 继续服务
/// </summary>
/// <param name="scname"></param>
void XServiceResume(string scname)
{
ServiceController sc = new ServiceController(scname);
ServiceControllerStatus st = sc.Status;
switch (st)
{
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.PausePending:
sc.Continue();
sc.WaitForStatus(ServiceControllerStatus.Running);
break;
default: break;
}
} #endregion #region 判断服务当前的运行状态
/// <summary>
/// 判断服务当前的运行状态
/// </summary>
/// <param name="scname"></param>
/// <returns></returns>
string XServiceStatus(string scname)
{
ServiceController sc = new ServiceController(scname);
ServiceControllerStatus st = sc.Status;
string result = string.Empty;
switch (st)
{
case ServiceControllerStatus.Running:
//运行中
result = "运行中";
break;
case ServiceControllerStatus.Paused:
result = "已暂停";
break;
case ServiceControllerStatus.Stopped:
result = "已停止";
break;
case ServiceControllerStatus.ContinuePending:
result = "即将继续";
break;
case ServiceControllerStatus.PausePending:
result = "即将暂停";
break;
case ServiceControllerStatus.StartPending:
result = "正在启动";
break;
case ServiceControllerStatus.StopPending:
result = "正在停止";
break;
default:
result = "错误";
break;
}
return result;
} #endregion

对于Service服务的“开启”,“停止”操作,创建的Service服务程序默认是支持的。而“暂停”,“恢复”操作,需要在服务的属性中进行开启设置才可以。

[开发笔记]-控制Windows Service服务运行

CanPauseAndContinue          服务是否接受暂停或继续运行的请求

在win7及以上系统上操作时,需要以管理员身份运行才能操作Service服务,否则程序会异常。如何让程序获得管理员权限,可以查看我前一篇文章:

C#如何以管理员身份运行程序 - 酷小孩 - 博客园

对于Service服务程序的创建操作,可以查看我之前的文章:

[开发笔记]-WindowsService服务程序开发 - 酷小孩 - 博客园

转载请注明出处。