zoukankan      html  css  js  c++  java
  • windows服务的停止和开启

     /// <summary>
    /// WindowsServiceManage
    /// </summary>
    public class WindowsServiceManage
    {
    /// <summary>
    /// 重启服务
    /// </summary>
    /// <param name="serviceName"></param>
    /// <returns></returns>
    public static bool RestartWindowsService(string serviceName)
    {
    bool bResult = false;
    try
    {
    try
    {
    StopWindowsService(serviceName);
    Thread.Sleep(
    1000);
    }
    catch (Exception ex)
    {
    StartWindowsService(serviceName);
    Thread.Sleep(
    1000);
    StopWindowsService(serviceName);
    Thread.Sleep(
    1000);
    Console.WriteLine(ex.Message);
    }
    try
    {
    StartWindowsService(serviceName);
    Thread.Sleep(
    1000);
    }
    catch (Exception ex) //C#启动Windows服务及关闭
    {
    StopWindowsService(serviceName);
    Thread.Sleep(
    1000);
    StartWindowsService(serviceName);
    Thread.Sleep(
    1000);
    Console.WriteLine(ex.Message);
    }
    bResult
    = true;
    }
    catch (Exception ex)
    {
    bResult
    = false;
    throw ex;
    }
    return bResult;
    }


    /// <summary>
    /// 停止服务
    /// </summary>
    /// <param name="serviceName">服务名称</param>
    /// <returns></returns>
    public static bool StopWindowsService(string serviceName)
    {
    ServiceController[] scs
    = ServiceController.GetServices();
    bool bResult = false;
    foreach (ServiceController sc in scs)
    {
    if (sc.DisplayName == serviceName)
    {
    try
    {
    sc.WaitForStatus(ServiceControllerStatus.Running,
    TimeSpan.FromSeconds(
    30));
    sc.Stop();
    bResult
    = true;
    }
    catch (Exception ex)
    {
    bResult
    = false;
    throw ex;
    }
    }
    }
    return bResult;
    }

    /// <summary>
    /// 启动服务
    /// </summary>
    /// <param name="serviceName">服务名称</param>
    /// <returns></returns>
    public static bool StartWindowsService(string serviceName)
    {
    ServiceController[] scs
    = ServiceController.GetServices();
    bool bResult = false;
    foreach (ServiceController sc in scs)
    {
    if (sc.DisplayName == serviceName)
    {
    try
    {
    sc.WaitForStatus(ServiceControllerStatus.Stopped,
    TimeSpan.FromSeconds(
    30));
    sc.Start();
    bResult
    = true;
    }
    catch (Exception ex)
    {
    bResult
    = false;
    throw ex;
    }
    }
    }
    return bResult;
    }
    }

      

  • 相关阅读:
    MonkeyScript_API
    APP性能(Monkey)【启动时间、CPU、流量、电量、内存、FPS、过度渲染】
    adb基本命令 & Monkey发生随机事件命令及参数说明
    MonkeyRunner_API
    2021春招冲刺-1218 页面置换算法 | sort的原理 | 语义化标签 | 标签的继承
    2021春招冲刺-1217 线程与进程 | ES6语法 | h5新增标签
    2021春招冲刺-1216 死锁 | 箭头函数 | 内联元素 | 页面渲染
    【unity】旧世开发日志
    HTTP 与HTTPS 简单理解
    GET POST 区分
  • 原文地址:https://www.cnblogs.com/yannis/p/2107053.html
Copyright © 2011-2022 走看看