zoukankan      html  css  js  c++  java
  • C#Windows服务:一些方法(启动、停止等)

      前面讲述了如何创建和安装服务(创建windows服务),下面把启动、停止、判断是否启动的方法也写一下。

           /// <summary>
            /// 判断是否安装了某个服务
            /// </summary>
            /// <param name="serviceName"></param>
            /// <returns></returns>
            public static bool ISWindowsServiceInstalled(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            return true;
                        }
                    }
    
    
                    return false;
                }
                catch
                { return false; }
            }
    

      

       /// <summary>
            /// 启动某个服务
            /// </summary>
            /// <param name="serviceName"></param>
            public static void StartService(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            service.Start();
    
    
                            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                    }
                }
                catch { }
            }
    

      

    /// <summary>
            /// 停止某个服务
            /// </summary>
            /// <param name="serviceName"></param>
            public static void StopService(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            service.Stop();
    
    
                            service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                    }
                }
                catch { }
            }
    

      

           /// <summary>
            /// 判断某个服务是否启动
            /// </summary>
            /// <param name="serviceName"></param>
            public static bool ISStart(string serviceName)
            {
                bool result = true;
    
    
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
    
    
                    foreach (ServiceController service in services)
                    {
                        if (service.ServiceName == serviceName)
                        {
                            if ((service.Status == ServiceControllerStatus.Stopped)
                                || (service.Status == ServiceControllerStatus.StopPending))
                            {
                                result = false;
                            }
                        }
                    }
                }
                catch { }
    
    
                return result;
            }
    

      原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/12126423

  • 相关阅读:
    js导出excel增加表头、mso-number-format定义数据格式
    $.each() 与 $(selector).each()的区别
    【Linux常用命令】Linux kill, killall, kill -9,
    Shell 语法报错记录
    【常用脚本记录1----挂测问题脚本】
    【】系统一启动就会运行的程序
    【WAN】PPPOE宽带上网功能详解
    解决linux awk:line的问题
    Ubuntu 16.04安装测试MQTT Mosquitto
    【TCP协议详解】
  • 原文地址:https://www.cnblogs.com/CSharpLover/p/6016430.html
Copyright © 2011-2022 走看看