从Windows服务关闭一个子进程个子、进程、Windows

2023-09-06 19:30:07 作者:光棍节@

我已经写了启动其在后台运行其他不同(子)进程在C#中的服务。我怎样才能在一个优雅的方式结束一个进程? Process.kill()不是一个选项, Process.CloseMainWindow()不起作用,因为子进程不可见的(也不应该有任何)。

I've written a service in c# that starts different other (child)processes which run in background. How can I close a process in a graceful way? Process.kill() is not an option, and Process.CloseMainWindow() does not work because the child process has no visible window (and shouldn't have any) .

坦克对您有所帮助。

推荐答案

如果你控制的其他过程中,你可以使用WCF进程之间的通信,子进程应检查并询问服务是否是时候结束进程,然后子进程做关闭并执行所需的清理。这是一个干净,简单的例子

If you control that other process you can use WCF to communicate between processes, child process should check and ask service if is time to end process and then child process can do close and do needed cleanup. Here is a clean and simple example

的http://www.switchonthe$c$c.com/tutorials/wcf-tutorial-basic-interprocess-communication