我想检测目标进程是否已经结束。我在下面写了预期的顺序:
Status.Text = "Running"
表示进程正在运行。Status.Text = "Finished"
。不幸的是,发布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
考虑使用Process.Exited事件。