testimonial
testimonial

Reputation: 103

How to Detect If Target Process Has Ended?

I want to detect if a target process has ended or not. I have written the expected sequence below:

  1. A process named TEST runs at the background.
  2. Status.Text = "Running" to indicate process is running.
  3. Process ends by itself.
  4. Status.Text = "Finished" right after the process ends.

Upvotes: 0

Views: 301

Answers (2)

KekuSemau
KekuSemau

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

Zev Spitz
Zev Spitz

Reputation: 15307

Consider using the Process.Exited event.

Upvotes: 0

Related Questions