.NET Process.Kill() 以安全的方式
我正在从 VB.NET GUI 控制一个老旧的 FORTRAN 模拟器,使用重定向 I/O 与模拟器可执行文件进行通信。 GUI 会弹出一个“状态”窗口,其中包含进度条、预计时间和“停止”按钮 (Button_Stop
)。
现在,我希望 Button_Stop 立即终止模拟器进程。执行此操作的明显方法是在 Child
Process 对象上调用 Kill()
。如果在进程退出后完成,则会出现异常,但我可以在尝试杀死进程之前测试进程是否退出,对吧?
好的,所以当单击按钮时我会执行以下操作:
If Not Child.HasExited Then
Child.Kill()
Button_Stop.Enabled = False
End If
但是,如果进程在测试和调用 Kill()
之间碰巧退出怎么办?在这种情况下,我会得到一个例外。
接下来我想到的是,我可以在 Process.Exited 事件处理程序中执行 Button_Stop.Enabled = False ,从而防止 Child.Kill( ) 在 Button_Stop.Clicked
处理程序中调用。但由于 Process.Exited 处理程序是在不同的线程上调用的,因此仍然会出现以下可能的交错:
- 子进程退出。
Process.Exited
触发,调用Invoke
来安排Button_Stop.Enabled = False
- 用户点击
Button_Stop
,触发 < code>Child.Kill() Button_Stop.Enabled = False
确实发生了。
然后,第 3 步将引发异常。
如何在没有任何竞争条件的情况下终止该进程?我的想法完全错误吗?
I'm controlling a creaky old FORTRAN simulator from a VB.NET GUI, using redirected I/O to communicate with the simulator executable. The GUI pops up a "status" window with a progress bar, estimated time, and a "STOP" button (Button_Stop
).
Now, I want the Button_Stop
to terminate the simulator process immediately. The obvious way to do this is to call Kill()
on the Child
Process object. This gives an exception if it's done after the process has exited, but I can test whether the process is exited before trying to kill it, right?
OK, so I do the following when the button is clicked:
If Not Child.HasExited Then
Child.Kill()
Button_Stop.Enabled = False
End If
However, what if the process happens to exit between the test and the call to Kill()
? In that case, I get an exception.
The next thing to occur to me was that I can do Button_Stop.Enabled = False
in the Process.Exited
event handler, and thus prevent the Child.Kill()
call in the Button_Stop.Clicked
handler. But since the Process.Exited
handler is called on a different thread, that still leaves the following possible interleaving:
- Child process exits.
Process.Exited
fires, callsInvoke
to schedule theButton_Stop.Enabled = False
- User clicks on
Button_Stop
, triggeringChild.Kill()
Button_Stop.Enabled = False
actually happens.
An exception would then be thrown on step 3.
How do I kill the process without any race conditions? Am I thinking about this entirely wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需捕获异常并在
finally
中禁用按钮即可:与其捕获所有类型的异常,当然最好只捕获
InvalidOperationException
和Win32Exception
> 因为如果进程正在终止或已经退出,则会抛出这些异常。您可能认为如果程序中发生异常是一件“坏事”,并且您应该设计程序来完全避免异常。然而,有不同类型的异常和异常处理,有些是错误的设计决策,而另一些(例如这个)是强制性的,因为异常的原因(即另一个进程的终止)超出了您的控制范围。
如果您想进一步阅读,我推荐您 Eric Lipperts 关于不同类型异常的帖子:
Simply catch the exception and disable the button in
finally
:Instead of catching all types of exceptions it would of course be better to only catch
InvalidOperationException
andWin32Exception
as these are thrown if the process is terminating or already exited.You probably think it is a "bad thing" if exceptions occur in a program and that you should design your program to avoid exceptions at all. However, there are different types of exceptions and exception handling, some being bad design decisions, and others - like this one - being mandatory, as the reason for the exception (i.e. the termination of another process) is out of your control.
If you want to read further I recommend you Eric Lipperts posts on different kinds of exceptions:
您可以 P/Invoke 到
TerminateProcess
中,如果进程已经退出,则不会抛出异常:You can P/Invoke into
TerminateProcess
which won't throw if the process has already exited: