.NET Process.Kill() 以安全的方式

发布于 2024-08-31 03:34:52 字数 1001 浏览 3 评论 0原文

我正在从 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 处理程序是在不同的线程上调用的,因此仍然会出现以下可能的交错:

  1. 子进程退出。
  2. Process.Exited 触发,调用 Invoke 来安排 Button_Stop.Enabled = False
  3. 用户点击 Button_Stop,触发 < code>Child.Kill()
  4. 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:

  1. Child process exits.
  2. Process.Exited fires, calls Invoke to schedule the Button_Stop.Enabled = False
  3. User clicks on Button_Stop, triggering Child.Kill()
  4. 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 技术交流群。

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

发布评论

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

评论(2

苍景流年 2024-09-07 03:34:53

只需捕获异常并在 finally 中禁用按钮即可:

Try                    
    Child.Kill()
Catch ex As Exception 
    MsgBox(ex.ToString())
Finally
    Button_Stop.Enabled = False
End Try

与其捕获所有类型的异常,当然最好只捕获 InvalidOperationExceptionWin32Exception > 因为如果进程正在终止或已经退出,则会抛出这些异常。

您可能认为如果程序中发生异常是一件“坏事”,并且您应该设计程序来完全避免异常。然而,有不同类型的异常和异常处理,有些是错误的设计决策,而另一些(例如这个)是强制性的,因为异常的原因(即另一个进程的终止)超出了您的控制范围。

如果您想进一步阅读,我推荐您 Eric Lipperts 关于不同类型异常的帖子:

编码中的奇妙冒险:烦恼例外

Simply catch the exception and disable the button in finally:

Try                    
    Child.Kill()
Catch ex As Exception 
    MsgBox(ex.ToString())
Finally
    Button_Stop.Enabled = False
End Try

Instead of catching all types of exceptions it would of course be better to only catch InvalidOperationException and Win32Exception 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:

Fabulous Adventures In Coding: Vexing exceptions

木槿暧夏七纪年 2024-09-07 03:34:53

您可以 P/Invoke 到 TerminateProcess 中,如果进程已经退出,则不会抛出异常:

Sub Main()
    Dim p = Process.Start("C:\Windows\system32\notepad.exe")
    Thread.Sleep(1000)
    TerminateProcess(p.Handle, 0)
    TerminateProcess(p.Handle, 0) ''# This call won't throw, it will just fail silently.
End Sub

<DllImport("kernel32.dll", SetLastError:=True)>
Private Function TerminateProcess(ByVal hProcess As IntPtr, ByVal uExitCode As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

You can P/Invoke into TerminateProcess which won't throw if the process has already exited:

Sub Main()
    Dim p = Process.Start("C:\Windows\system32\notepad.exe")
    Thread.Sleep(1000)
    TerminateProcess(p.Handle, 0)
    TerminateProcess(p.Handle, 0) ''# This call won't throw, it will just fail silently.
End Sub

<DllImport("kernel32.dll", SetLastError:=True)>
Private Function TerminateProcess(ByVal hProcess As IntPtr, ByVal uExitCode As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文