Reputation: 103
I want to detect if a target process has ended or not. I have written the expected sequence below:
Status.Text = "Running"
to indicate process is running.Status.Text = "Finished"
right after the process ends.Upvotes: 0
Views: 301
Reputation: 6856
Unfortunately, the solution posted here requires to be run as administrator.
A simple polling-solution using a timer could do the work just fine.
If you use a polling solution, then of course you have to re-read the processes inside the loop or polling event.
Use the process name without .exe
here.
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
Upvotes: 1