c# - 从另一个服务停止服务

标签 c# windows-services

我有一项服务仅用于启动和停止我创建的另一项服务。 我遇到的问题是,当我尝试从名为 TUTController 的 Controller 服务中停止名为 TUT 的其他服务时,它会自行停止而不是预期的服务。

  public TUTController()
    {
        InitializeComponent();
        observer = new StatusObserver();
        observer.Update += new EventHandler<CustomArgs>(observer_Update);
    }

    void observer_Update(object sender, CustomArgs e)
    {
        ServiceController sc = new ServiceController("TUT");
        sc.Refresh();

        switch (e.CurrentStatus)
        {
            case StatusObserver.Status.On:
                if (sc.Status == ServiceControllerStatus.Stopped)
                {
                    sc.Start();
                    sc.Refresh();
                }
                break;
            case StatusObserver.Status.Off:
                if (sc.Status == ServiceControllerStatus.Running)
                {
                    sc.Stop();
                    sc.Refresh();
                }
                break;
            default:
                break;
        }
    }

观察者是一个类,它经常检查数据库中的 Off/On 值并通过 Update 事件将其发布回来。提前致谢。

最佳答案

我建议检查这两个服务的名称和依赖关系。

您确定 TUT & TUTController 是您认为的服务名称吗?

此外,SCM 是否有可能将 TUT 列为 TUTController 的依赖项(而不是相反)并因此导致它关闭?

关于c# - 从另一个服务停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5545046/

相关文章:

c# - 诸如 Mathf.Ceil 之类的任何东西都可以理解数字的大小,无论是 +/- 吗?

c# - 将 Excel 范围转换为 ADO.NET 数据集或数据表等

c# - 非对称加解密

c# - 将一堆位图转储为 PDF 的最快的 .Net PDF 库是什么?

vb6 - 更新: Interacting with the user on the windows logon screen

java - java servicewrapper 的替代品?

c# - Windows服务调试错误

c# - 安装服务到服务器 2012

c# - .NET 电子邮件处理建议

c# - void 方法中的 return 语句