有没有办法确保当我的进程终止时,我的程序生成的后台进程被杀死?
基本上,子进程无限期地运行,直到在后台被杀死,当我的程序因任何原因终止时,我想清理它,即通过任务管理器。
目前,如果父进程没有运行,我有一段时间 (Process.GetProcessesByName("ParentProcess").Count() > 0) 循环并退出,但它看起来很脆弱,如果我希望它在调试器下工作Visual Studio 我必须添加“ParentProcess.vshost”或其他内容。
有没有什么方法可以确保子进程结束而不要求子进程知道父进程? 我更喜欢托管代码中的解决方案,但如果没有,我可以 PInvoke。
编辑:传递 PID 似乎是一个更强大的解决方案,但出于好奇,如果子进程不是我的代码而是一些我无法控制的 exe 怎么办? 有没有办法防止可能创建孤立的子进程?
Basically the child process runs indefinitely until killed in the background, and I want to clean it up when my program terminates for any reason, i.e. via the Taskmanager.
Currently I have a while (Process.GetProcessesByName("ParentProcess").Count() > 0) loop and exit if the parent process isn't running, but it seems pretty brittle, and if I wanted it to work under debugger in Visual Studio I'd have to add "ParentProcess.vshost" or something.
Is there any way to make sure that the child process end without requiring the child process to know about the parent process? I'd prefer a solution in managed code, but if there isn't one I can PInvoke.
Edit: Passing the PID seems like a more robust solution, but for curiosity's sake, what if the child process was not my code but some exe that I have no control over? Is there a way to safeguard against possibly creating orphaned child processes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果子进程是您自己的代码,则可以在启动它时将父进程的 PID 传递给它。 然后,子进程可以使用
获取进程Process.GetProcessById
并订阅其Exited
事件,带有一个处理程序,可以正常关闭(子)进程的其余部分。 请注意,您需要设置EnableRaisingEvents<进程上的 /code>
属性设置为
true
。If the child process is your own code, you could pass it the PID of the parent process when you launch it. The child process could then fetch the process with
Process.GetProcessById
and subscribe to itsExited
event with a handler which shuts down the rest of the (child) process gracefully. Note that you need to set theEnableRaisingEvents
property on the process totrue
.如果子进程不是您自己的代码,您可以使用以下代码查找并终止所有子进程:
If the child process is not your own code you can use this code to find and kill all child processes:
此类子进程的通用术语为孤儿进程。 请参阅链接的文章了解一些可能的解决方案。
The common term for such a child process in as orphan process. See the linked article for some possible solutions.
这是我构建的一个小型实用程序应用程序的源代码
(它基于 Alan Hensel 解决方案,我发现它非常有用)。
它被称为ChildrenProcessKiller,它是一个观察程序,可以在父进程退出时杀死给定父进程的所有后代进程(即使父进程崩溃)
用法:
警告:此代码“按原样”提供,并且可能杀死小婴儿;-)
ChildrenProcessKiller.cs
ProcessTree.cs
Here a the source code for a small utility app I built
(it is based on Alan Hensel solution, which I found quite useful).
It is called ChildrenProcessKiller and it is a watcher that enables to kill all descendants process of a given parent process when the parent process exits (even if the parent process crashes)
Usage :
Warning : this code is provided "as is", and it might kill small babies ;-)
ChildrenProcessKiller.cs
ProcessTree.cs
将父进程 ID 作为命令行参数传递给子进程。
在子进程中使用 get Process by id 并订阅它的 Exit 事件或创建一个线程并调用 Process.WaitForExit
Pass the parent process id as command line parameter to child process.
In child process use get Process by id and subscribe to it's Exit event or create a thread and call to Process.WaitForExit