进程拒绝消亡
基于其他问题,我使用System.Diagnostics.Process
来Start
和 Kill
进程:
this.childProcess.Kill();
this.childProcess.WaitForExit();
this.childProcess.Close();
我假设 WaitForExit
处理 Kill
命令的异步性质,但是有问题的进程(perl.exe)拒绝终止。相反,它会持续一段时间。
这个“时间段”导致了竞争条件。无论如何,我可以确保这个进程确实终止吗?
Based on other questions, I'm using System.Diagnostics.Process
to Start
and Kill
a process:
this.childProcess.Kill();
this.childProcess.WaitForExit();
this.childProcess.Close();
I would assume that WaitForExit
deals with the asynchronous nature of the Kill
command, however the process in question (perl.exe) refuses to die. Instead it lingers for some period of time.
This "period of time" is causing a race condition. Is there anyway I can make sure this process actually dies?
我不确定你所说的“确保这个进程实际上消失”是什么意思。在底层,
Kill
方法最终调用 TerminateProcess 这是终止进程的最有效方法,并且WaitForExit
在进程实际消失之前不会返回。Kill
方法至少在两种情况下无法实际终止进程这些是否适用于您的场景?
I'm not sure what you mean by "make sure this process actually dies." Under the hood the
Kill
method eventually calls TerminateProcess which is the most effective way to kill a process andWaitForExit
won't return until the process is actually gone.The
Kill
method can fail to actually kill the process in at least 2 circumstancesDo either of these apply to your scenario?