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();
    }  
    
  • 相关阅读:
    Unity调试模式设置辅助线是否可见
    Gizmos绘制塔防游戏网格
    JS offsetparent 问题
    JS 图像延迟加载
    JS image对象
    JS 瀑布流
    JS 对象
    JS node
    Vue+element 实现表格的增加行、根据索引删除行的功能
    Java的集合框架
  • 原文地址:https://www.cnblogs.com/le0zh/p/4283767.html
Copyright © 2011-2022 走看看