• 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;
    }
    }

      

  • 相关阅读:
    Python3.x:定义一个类并且调用
    Spring编码过滤器:解决中文乱码
    Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
    你是否属于中等收入群体
    Python3.x:BeautifulSoup()解析网页内容出现乱码
    Activiti工作流引擎数据库表结构
    Java:出现错误提示(java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date)
    Django框架搭建(windows系统)
    Eclipse配置多个jdk
    Activiti:创建activiti工程
  • 原文地址:https://www.cnblogs.com/yannis/p/2107053.html
Copyright © 2020-2023  润新知