终止子进程

发布于 2024-12-03 10:09:02 字数 256 浏览 7 评论 0原文

我有一个 C# 应用程序,它启动一些子进程并重定向它们的标准输入和输出。

如果我以正常方式关闭应用程序,所有子进程都将被销毁,但如果应用程序被任务管理器或其他方式杀死,我将无法关闭子进程。

它们存在并且需要大量的 CPU 能力(接近 99%)。我认为他们需要如此多的力量,因为他们的“重定向”标准输出没有被读取?

有没有办法始终关闭子进程?或者在杀死父进程后减少它们的CPU时间?

谢谢&最好的问候

凯文·梅尔

I have a C#-Application, which starts some child-Processes and redirect their Standart-Input and Output.

If i close the application on the normal way all child processes will be destroyed, but if the application is killed by the Task Manager or another way i can't close the child-processes.

They exist and need much CPU-Power (Nearly 99%). I think they need so much Power because their "redirected" Standart-Output is not readed?

Is there a way to close the child-processes always? Or to decrease their CPU-Time after killing the parent-process?

thanks & best regards

Kevin Meier

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兮颜 2024-12-10 10:09:02

简短的回答是否定的!

当用户终止您的应用程序进程时,它就终止了。您对此无能为力,除非您拥有子进程源代码并将其修改为在父进程终止后结束,或者在应用程序终止时使用单个观察者进程来终止应用程序的子进程。

The short answer is No!

When user terminates your application process, It's just terminated. You can't do anything about it, unless you have the child processes source code and modify them to end after parent termination or use an individual observer process to terminate child processes of your application when it's terminated.

盗琴音 2024-12-10 10:09:02

重定向流本身不应导致 CPU 使用率过高,可能会发生其他情况。

Redirecting the streams shouldnt cause high cpu usage by it self, something else is probably happening.

沉溺在你眼里的海 2024-12-10 10:09:02

如果我遇到这个问题,我有时会在所有子进程中检查正在运行的父进程,如果父进程不存在,子进程将退出。

If i had this problem, i would check running parent proccess sometimes in all child processes if parent process dont exist, child process would do exit.

我喜欢麦丽素 2024-12-10 10:09:02

如果要保证子进程在父进程终止时也结束,可以使用 job对象。不幸的是,没有简单的方法可以从托管代码中执行此操作。它需要对 Win32 API 进行大量 P/Invoke 互操作调用,例如 CreateJobObject, AssignProcessToJobObject等。但一般来说应该是可以的。

If you want to guarantee that child processes end when the parent is terminated, you can use job objects. Unfortunately, there is no simple way to do this from managed code; it would require a lot of P/Invoke interop calls to Win32 APIs like CreateJobObject, AssignProcessToJobObject, etc. But in general it should be possible.

赠佳期 2024-12-10 10:09:02

您可以使用命名管道等在父进程和子进程之间进行通信,不断检查父进程是否存在。

You can use named pipe etc to communicate between parent and child processes, keep checking if the parent is there or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文