简体   繁体   English

如何在没有“进程退出”异常的情况下终止进程?

[英]How to kill a process without getting a “process has exited” exception?

I use Process.Kill() to kill a process. 我使用Process.Kill()来杀死进程。 Like this: 像这样:

if( !process.WaitForExit( 5000 ) ) {
   process.Kill();
}

and sometimes the process will exit right in between the lines, so control will get inside if and then Kill will yield an exception: 有时进程将在行之间退出,因此if Kill将导致异常,则控制将进入内部:

System.InvalidOperationException
Cannot process request because the process (ProcessIdHere) has exited.
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.Kill()
//my code here

Now wrapping the code into try-catch doesn't seem to be a good idea because InvalidOperationException can be called for other reasons. 现在将代码包装到try-catch中似乎不是一个好主意,因为可以出于其他原因调用InvalidOperationException

Is there a way to kill a process without getting an exception in the described scenario? 有没有办法在所描述的场景中杀死进程而不会出现异常?

You could P/Invoke TerminateProcess passing it Process.Handle . 你可以P / Invoke TerminateProcess传递它Process.Handle Then manually evaluating the cause of it ( GetLastError() ). 然后手动评估它的原因( GetLastError() )。 Which is roughly, what Process.Kill() does internally. 粗略地说, Process.Kill()在内部做什么。

But note that TerminateProcess is asynchronous. 但请注意, TerminateProcess是异步的。 So you'd have to wait on the process handle to be sure it is done. 因此,您必须等待进程句柄以确保完成。 Using Process.Kill() does that for your. 使用 Process.Kill()为您做到这一点。

Update: Correction, Process.Kill() also runs asynchronously. 更新:更正, Process.Kill()也异步运行。 So you'll have to use WaitForExit() to wait for termination to complete - if you care. 所以你必须使用WaitForExit()等待终止完成 - 如果你在意的话。

Frankly, I wouldn't bother. 坦率地说,我不会打扰。 Of course there is always the (remote?) chance that some "arbitrary" InvalidOperationExcepion bubbles up out of that line of code, that is not related to the process no longer being there or the Process object to be in an invalid state, but in reality I think you can just go with the try/catch around the Kill . 当然,总是存在(远程?)机会,某些“任意”的InvalidOperationExcepion从该行代码中冒出来,这与不再存在的ProcessProcess对象处于无效状态无关,而是现实我认为你可以选择围绕Kill的尝试/捕获。

In addition, depending on your application, you could consider logging this kill anyway, since it seems some sort of last resort measure. 此外,根据您的应用程序,您可以考虑记录此杀死,因为它似乎是某种最后的手段。 In that case log the actual InvalidOperationException with it. 在这种情况下,使用它记录实际的InvalidOperationException If things go strange, you at least have your logs to check why the Kill failed. 如果事情变得奇怪,你至少要有你的日志来检查Kill失败的原因。

Having all that said, you might also want to consider catching / handling Win32Exception for the same reasons. 尽管如此,您可能还需要考虑捕获/处理Win32Exception ,原因相同。

You need to use HasExited : 您需要使用HasExited

if(!process.WaitForExit(5000)) 
{
   if (!process.HasExited)
   {
       process.Kill();
   }
}

See here: 看这里:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM