【问题标题】:How to Run an exe from windows service and stop service when the exe process quits?exe进程退出时如何从Windows服务运行exe并停止服务?
【发布时间】:2010-09-15 15:22:20
【问题描述】:

我完全是使用 Windows 服务的初学者。我已经为该服务制定了一个基本框架,我目前正在这样做:

protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        Process.Start(@"someProcess.exe");
    }

只是为了在程序开始时关闭 exe。

但是,当从 exe 启动的进程退出时,我希望服务自行停止。 我很确定我需要做某种线程(我也是初学者),但我不确定它是如何工作的总体轮廓,也不确定从内部停止进程的具体方法。 你能帮我解决这个问题的一般过程吗(即从 OnStart 开始一个线程,然后……?)?谢谢。

【问题讨论】:

    标签: c# multithreading windows-services


    【解决方案1】:

    您可以使用BackgroundWorker 进行线程处理,使用Process.WaitForExit() 等待进程终止,直到您停止服务。

    你是对的,你应该做一些线程,在OnStart 中做很多工作可能会导致在启动服务时无法从 Windows 正确启动的错误。

    protected override void OnStart(string[] args)
    {
    
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();
    }
    
    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo("file.exe");
        p.Start();
        p.WaitForExit();
        base.Stop();
    }
    

    编辑 您可能还想将Process p 移动到类成员并停止OnStop 中的进程,以确保在exe 失控时可以再次停止服务。

    protected override void OnStop()
    {
        p.Kill();
    }
    

    【讨论】:

      【解决方案2】:

      您必须使用ServiceController 来执行此操作,它有一个Stop 方法。确保您的服务将 CanStop 属性设置为 true。

      【讨论】:

        【解决方案3】:

        someProcess.exe 应该有 someLogic 来停止调用服务;)

        使用ServiceController 类。

        // Toggle the Telnet service - 
        // If it is started (running, paused, etc), stop the service.
        // If it is stopped, start the service.
        ServiceController sc = new ServiceController("Telnet");
        Console.WriteLine("The Telnet service status is currently set to {0}", 
                          sc.Status.ToString());
        
        if  ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
             (sc.Status.Equals(ServiceControllerStatus.StopPending)))
        {
           // Start the service if the current status is stopped.
        
           Console.WriteLine("Starting the Telnet service...");
           sc.Start();
        }  
        else
        {
           // Stop the service if its status is not set to "Stopped".
        
           Console.WriteLine("Stopping the Telnet service...");
           sc.Stop();
        }  
        
        // Refresh and display the current service status.
        sc.Refresh();
        Console.WriteLine("The Telnet service status is now set to {0}.", 
                           sc.Status.ToString());
        

        代码来自上面链接的页面。

        【讨论】:

        • 谢谢,这看起来确实会完成最终结果 - 如果我也在开发 exe。但是,这不是一种解决方法而不是解决方案吗?并且它只会在我正在编写 .exe 的情况下工作。假设我无法访问 exe 的源代码,这仍然可以通过 Windows 服务本身实现。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 2011-07-15
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多