3

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?

william
  • 31
  • 1
  • 4

2 Answers2

4

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)

gregmac
  • 24,276
  • 10
  • 87
  • 118
  • The namespace is Microsoft.VisualBasic, NOT Microsoft.VisualBasic.Compatibility. So there is NO problem using it. The Microsoft.VisualBasic namespace is absolutely, 100% true .Net, fully supported, and will be around as long as .Net is around. See answer to this question: http://stackoverflow.com/questions/226517/is-the-microsoft-visualbasic-namespace-true-net-code – Stefan Nov 09 '10 at 14:32
  • That said, I would still use the process.start method that gives lots of more options. – Stefan Nov 09 '10 at 14:34
1

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

Stefan
  • 11,423
  • 8
  • 50
  • 75
  • but i want the shell to run and VB.net to wait untill the shell is done (i want it just the other way around) – william Nov 09 '10 at 14:21
  • This WILL make the VB-net wait for the process to be done before continuing the execution. – Stefan Nov 09 '10 at 14:23
  • correction i should of tested it out first you are more then correct Stefan it just looks at first sight that im making the bat wait but thats obviously not the case as is now proven. my mistake thankyou very much – william Nov 09 '10 at 14:31