Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pause/ wait for *.bat to finish VB.net

Tags:

vb.net

hi i need to pause/ wait the bellow VB program between the arorasTEMP.bat and the "Label2.Text = "Appending for AutoCAD version A..." as it happens the bat is appending before its temp copy is made

    Dim RetBat1

    RetBat1 = Shell("C:\VTS\arorasTEMP.bat", 1)

    Label2.Text = "Appending for AutoCAD version A..."

    'Appending the acad2011.lsp
    If System.IO.File.Exists(FILE_NAME1) = True Then
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME1, True)
        objWriter.WriteLine(arorasKEY)
        objWriter.Close()

    End If

can anyone give example?

like image 896
william Avatar asked Dec 17 '25 01:12

william


2 Answers

Shell is a VB6 command, it's not the ideal way to launch processes.

The proper way in .NET to invoke a process and wait for it is:

Dim aroras as Process = Process.Start("C:\VTS\arorasTEMP.bat")
aroras.WaitForExit()
' error code is available in aroras.ExitCode, if you need it 

You can also forcibly kill it if it takes too long:

If Not aroras.WaitForExit(300) Then
   aroras.Kill()
End If

(where 300 is the time in milliseconds to wait)

like image 77
gregmac Avatar answered Dec 19 '25 17:12

gregmac


you can tell the shell to wait for the process to be done before executing anything else:

RetBat1 = Shell("C:\VTS\arorasTEMP.bat", ,True) 

And even give a timeout that stops the execution if it takes too long, in this case 6 seconds:

RetBat1 = Shell("C:\VTS\arorasTEMP.bat", , True, 6000) 

More info about the Shell function: http://msdn.microsoft.com/en-us/library/xe736fyk(VS.80).aspx

like image 29
Stefan Avatar answered Dec 19 '25 16:12

Stefan