testimonial testimonial - 4 years ago 80
Vb.net Question

Do function right after a proccess ends

Clicks on button


  1. TEST process runs at background.

  2. Label2.Text = "Running"

  3. TEST process ends automatically after the job done.

  4. Now this is the place I want to edit. I want my software change
    Label2.Text
    to
    "Finished"
    right after "TEST" process ends. How can I achieve this? Tried endless things.



SOLUTION:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
...
AddHandler timer_watcher.Tick, AddressOf TimerEvent
timer_watcher.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds
timer_watcher.Start()
End If
End Sub

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download