zoukankan      html  css  js  c++  java
  • 使用程序控制windows service启动/停止

    1.首先加入引用:

    using System.ServiceProcess;
    

    2.控制启动服务:

    public void Start()
    {
        var timeout = TimeSpan.FromSeconds(60);
        try
        {
            _serviceController.Start();
            _serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }
        catch (Exception ex)
        {
            throw new Exception("Service " + _serviceName + " is not started as expected, error: " + ex.Message);
        }
    }  
    

    3.控制停止服务:

    public void Stop()
    {
        var timeout = TimeSpan.FromSeconds(60);
        try
        {
            if (_serviceController.Status != ServiceControllerStatus.Stopped &&
                _serviceController.Status != ServiceControllerStatus.StopPending)
            {
                _serviceController.Stop();
                _serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Service " + _serviceName + " is not stoped as expected, error: " + ex.Message);
        }
    }  
    

    4.控制重启服务:

    public void Restart()
    {
        Stop();
        Start();
    }  
    
  • 相关阅读:
    第08讲树
    第11讲简单算法
    【ZOJ1004】Anagrams by Stack
    【ZOJ1649】Rescue
    第10讲并查集
    网站建设与网页制作课件
    获取鼠标的坐标
    asp.net页面的默认回车事件
    NeatUpload的安装使用
    Page methods 执行顺序
  • 原文地址:https://www.cnblogs.com/le0zh/p/4283767.html
Copyright © 2011-2022 走看看