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

问题描述 投票:0回答:2

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

  1. 名为TEST的进程在后台运行。
  2. Status.Text = "Running"表示进程正在运行。
  3. 流程本身就结束了。
  4. 在这个过程结束后,Status.Text = "Finished"
vb.net
2个回答
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

0
投票

考虑使用Process.Exited事件。

© www.soinside.com 2019 - 2024. All rights reserved.