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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13564645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:00:45  来源:igfitidea点击:

How to kill a process without getting a "process has exited" exception?

c#.netprocess

提问by sharptooth

I use Process.Kill()to kill a process. Like this:

Process.Kill()用来杀死一个进程。像这样:

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

and sometimes the process will exit right in between the lines, so control will get inside ifand then Killwill 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 InvalidOperationExceptioncan be called for other reasons.

现在将代码包装到 try-catch 中似乎不是一个好主意,因为InvalidOperationException可以出于其他原因调用。

Is there a way to kill a process without getting an exception in the described scenario?

在所描述的场景中,有没有办法杀死进程而不会出现异常?

采纳答案by Christian.K

You could P/Invoke TerminateProcesspassing it Process.Handle. Then manually evaluating the cause of it (GetLastError()). Which is roughly, what Process.Kill()does internally.

你可以 P/InvokeTerminateProcess传递它Process.Handle。然后手动评估其原因 ( GetLastError())。粗略地说,Process.Kill()内部做了什么。

But note that TerminateProcessis asynchronous. So you'd have to wait on the process handle to be sure it is done. Using Process.Kill()does that for your.

但请注意,这TerminateProcess是异步的。所以你必须等待进程句柄以确保它完成。使用Process.Kill()可以为您做到这一点。

Update:Correction, Process.Kill()also runs asynchronously. So you'll have to use WaitForExit()to wait for termination to complete - if you care.

更新:更正,Process.Kill()也异步运行。所以你必须使用WaitForExit()等待终止完成 - 如果你关心。

Frankly, I wouldn't bother. Of course there is always the (remote?) chance that some "arbitrary" InvalidOperationExcepionbubbles up out of that line of code, that is not related to the process no longer being there or the Processobject to be in an invalid state, but in reality I think you can just go with the try/catch around the Kill.

坦率地说,我不会打扰。当然,总是有(远程?)从那InvalidOperationExcepion行代码中冒出一些“任意”的机会,这与不再存在的过程或Process处于无效状态的对象无关,但实际上我认为你可以在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 InvalidOperationExceptionwith it. If things go strange, you at least have your logs to check why the Killfailed.

此外,根据您的应用程序,无论如何您都可以考虑记录此杀戮,因为这似乎是某种不得已的措施。在这种情况下,InvalidOperationException用它记录实际。如果事情变得奇怪,你至少有你的日志来检查Kill失败的原因。

Having all that said, you might also want to consider catching / handling Win32Exceptionfor the same reasons.

说了这么多,Win32Exception出于同样的原因,您可能还想考虑捕获/处理。

回答by Lloyd

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