wait for shell command

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Guys,

I am looking for a small bit of code that will launch a batch file and wait for that to complete then continue with the code.

Sudo code to try to better explain myself:

Run batch.bat
wait for batch.bat to stop running (loop maybe?)
If batch.bat not running/finished then
Run message box pop up ("Batch.bat has finished running)


Cheers,

James
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Dim proc As New Process
proc.StartInfo.FileName = "cmd.exe"
proc.StartInfo.Arguments = "batchfile.bat"
proc.StartInfo.UseShellExecute = False
proc.StartInfo.CreateNoWindow = True
proc.Start()
proc.WaitForExit(4000)
 

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
I was unable to get this working but I am sure it is just me.

I got this working however:

--------------------Code Start-----------------------------------

Dim sCommand As String

sCommand = "c:\dir.bat"
Dim p As Process = Process.Start(sCommand)
p.WaitForExit()
'Continue with the code.
MessageBox.Show("Code continuing...")

-------------------------Code End------------------------------
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: jameswhite1979
I was unable to get this working but I am sure it is just me.

I got this working however:

--------------------Code Start-----------------------------------

Dim sCommand As String

sCommand = "c:\dir.bat"
Dim p As Process = Process.Start(sCommand)
p.WaitForExit()
'Continue with the code.
MessageBox.Show("Code continuing...")

-------------------------Code End------------------------------

It doesn't work because your process takes longer than 4000 milliseconds to complete.

http://msdn.microsoft.com/en-u...ocess.waitforexit.aspx

If you don't pass an arg, the calling process will block until the subprocess is finished.

Not sure why he passed 4000 in his code. :confused: