PaulPolon
PaulPolon

Reputation: 329

vb.net check if process I started is done

I have started a process:

Dim getUdpate as Process
getUpdate = New Process
getUpdate.StartInfo.FileName = "C:\UTIL\GETBTCH.BAT"
getUpdate.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
getUpdate.StartInfo.UseShellExecute = False
getUpdate.StartInfo.WorkingDirectory = "C:\UTIL\"
getUpdate.Start()
getUpdate.Close()

Then, I want to Run another process but I want to check first if the getUpdate process is already finished.

How do I check if the process is already finished?

I already tried to look at the processes ID, but it only display cmd.exe and there are a lot of cmd.exe as the processes ID so I can't just go and stop all of those.

Upvotes: 4

Views: 6019

Answers (3)

Vincent Vancalbergh
Vincent Vancalbergh

Reputation: 3327

If you are making a WinForms application or similarly interactive UI I suggest hooking a function into the object's Exited event instead of polling HasExited.

(You may already know this but) if you use WaitForExit or poll HasExited your UI is hanging exactly because your code is actually waiting for the process to end.

Your UI only has one thread and cannot "multitask". That's why these "processing" type of actions should be done in a different thread (or, as is the case here, a different process) and report back to the UI when they finish.

Example:

' Handle Exited event and display process information.
Private Sub myProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs)
    'Do something in your UI
End Sub

and in your starting code:

getUpdate.EnableRaisingEvents = True
AddHandler getUpdate.Exited, AddressOf myProcess_Exited

Upvotes: 2

shf301
shf301

Reputation: 31394

You can check the HasExited property of the process. It will return true if the process has ended, and false if it is still running.

You will need to check this before you call Close() on your getUpdate Process object. So getProcess will have to remain open until the procsses has exited.

Upvotes: 6

David Tansey
David Tansey

Reputation: 6013

Try:

getUpdate.WaitForExit(); instead of

getUpdate.Close()

Upvotes: 2

Related Questions