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

      

  • 相关阅读:
    Ruby单例方法和实例方法
    Silverlight本地化和全球化
    多线程 or 多进程 (转强力推荐)
    循环pthread_create导致虚拟内存上涨
    int在linux上的保存情况
    查看数据流的流程
    查看linux系统版本,内核,CPU,MEM,位数的相关命令(转)
    0/1背包问题
    linux下计算程序运行时间
    夸平台夸字符编码问题
  • 原文地址:https://www.cnblogs.com/yannis/p/2107053.html
Copyright © 2011-2022 走看看