启动一个从属进程
我想使用 Process.Start() 从我的应用程序启动一个进程,并且我希望在我的应用程序崩溃或用户关闭它的情况下,子进程会自动终止。
有可能获得这样的东西吗?
I would like to start a process from my application with Process.Start() and I would like that in case of my app crash or the user close it then the child process is automatically killed.
Is it possible to obtain something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 AppDomain.ProcessExit 事件来捕获进程即将退出的时间并让它终止其他进程。我不确定它如何响应未处理的异常,因此您可能必须使用 AppDomain.UnhandledException 并让它使用 FailFast 来处理任何正常异常。作为该处理程序的一部分,您也可以让它杀死其他进程。
You can use the
AppDomain.ProcessExit
event to catch when your process is about to exit and have it kill the other process. I am unsure as to how it works in reaction to an unhandled exception, so you may have to useAppDomain.UnhandledException
and have it use FailFast to handle any normal exceptions. As part of that handler, you have it kill the other process too.您可以将启动的子进程的 PID 存储在某处,然后在应用程序关闭事件中使用该 PID 终止进程。尽管在未处理的崩溃的情况下,子进程可能会保留在内存中。
You could store PID of lauched child process somewhere, then kill process with that PID in application closing event. Though in case of unhandled crash the child process may retain in memory.