如何检测目标进程是否已结束?

时间:2015-05-10 15:24:00

标签: vb.net

我想检测目标进程是否已经结束。我在下面写了预期的顺序:

  1. 名为TEST的流程在后台运行。
  2. testSecurityFilter.authenticateNextRequestAs("username");表示进程正在运行。
  3. 流程自行结束。
  4. 在过程结束后立即
  5. Status.Text = "Running"

2 个答案:

答案 0 :(得分:1)

不幸的是,发布here的解决方案需要以管理员身份运行。

使用计时器的简单轮询解决方案可以正常工作 如果您使用轮询解决方案,那么当然您必须重新读取循环或轮询事件中的进程 在此处使用没有.exe的流程名称。

Private timer_watcher As Timer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.Label1.Text = "Watching"
    Me.timer_watcher = New Timer
    AddHandler timer_watcher.Tick, AddressOf TimerEvent
    timer_watcher.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds
    timer_watcher.Start()
End Sub

Public Sub TimerEvent(sender As Object, e As EventArgs)
    Dim p() As Process = System.Diagnostics.Process.GetProcessesByName("processname")
    If p.Length = 0 Then
        timer_watcher.Stop()
        Me.Label1.Text = "Stopped"
    End If
End Sub

答案 1 :(得分:0)

考虑使用Process.Exited事件。

相关问题