locked
Wait till DOS function finishes? RRS feed

  • Question

  • I finally figured out a problem that had been driving me nuts. I kept getting a "directory not found" error after extracting an archive with full path then trying to change to that directory. Every time I checked, the path was there just fine.

    The annoying thing was that the code worked perfectly when single-stepping with the debugger, but not at runtime, making it impossible to debug.

    After sleeping on it, it finally dawned on me that my program was not waiting for the extraction to finish before trying to switch to the extracted directory.

    So, how do I tell VB to wait until my dos command finishes before going on to the next command? (I'd rather not put a physical "wait" or "pause" in my code b/c not every machine extracts at the same speed.)

    Thx

    Saturday, October 4, 2014 11:10 AM

Answers

  • Found the code to force a wait till termination:

    Dim p As New Process
    Dim psi As New ProcessStartInfo("program.exe", "parameters")
    Dim np As New Process
    np.StartInfo = psi
    np.Start()
    np.WaitForExit()
    Sadly, it didn't solve my problem, but I'm probably on the right track.


    Saturday, October 4, 2014 11:49 AM
  • I am using a third-party codec to extract a proprietary archive format known as "txx". Code is inside a button_click event:

    ' Disabling this block of code has no effect on the outcome.
    If FileIO.FileSystem.DirectoryExists("c:\OVC") Then
       FileIO.FileSystem.DeleteDirectory("c:\OVC", _
       FileIO.DeleteDirectoryOption.DeleteAllContents) ' Start fresh
    FileIO.FileSystem.CreateDirectory("c:\OVC")
    WriteFile() ' Writes data to the destination folder. ' Extract "Extra.p004.txx" so we can COPY and modify "VcVoiceSets.L=EN.bs". txx_codec("extract", strTKPath & "\Archives\2.114.001\Extra.p004.txx") ' Process is in a module. Creates "C:\OVC\temp\Extra.p004\Scripts\Shared\file.txt" Dim strTemp As String = "C:\OVC\temp\Extra.p004\Scripts\Shared" My.Computer.FileSystem.CurrentDirectory = strTemp ' CRASH! Error: "Could not find file 'C:\OVC\temp\Extra.p004\Scripts\Shared' My.Computer.FileSystem.CopyFile(strTemp, "C:\OVC\temp\Scripts\Shared\Voice\VcTemp.txt") ' Operate on a copy.

    The code for the "txx_codec()" process, which works fine, looks like this:

        Public Sub txx_codec(ByVal strDirection As String, ByVal strFile As String)
            Dim p As New Process
            If strDirection = "extract" Then
                Dim psi As New ProcessStartInfo(strInstallPath & "\txx_tools.exe", "-x -k 1 " & Chr(34) & strFile & Chr(34) & " " & "c:\OVC\temp")
                Dim np As New Process
                np.StartInfo = psi
                np.Start()
                np.WaitForExit() ' Wait for codec to finish
                ' adding a pause here doesn't help.
            End If
            If strDirection = "compress" Then
                System.Diagnostics.Process.Start(strInstallPath & "\txx_tools.exe", "-b -k 1 " & "c:\OVC\temp" & " " & Chr(34) & strFile & Chr(34))
            End If
        End Sub

    I tried manipulating the archive to remove the period from the path, but it made no difference, so that wasn't this issue. I thought I had the answer when I realized that the program was probably looking for the path before the extraction finished, but waiting for it didn't change anything.

    I have confirmed that the path does indeed exist when it claims it doesn't, even copied/pasted the path from Explorer.

    I'm totally stumped.

    ADDENDUM: Ah! Talking it out helped! Parsing the code line-by-line, I realized that strTemp inside my "CopyFile" didn't include the filename, only the folder. VS reports the error on the wrong line!)

    Saturday, October 4, 2014 1:48 PM

All replies

  • Found the code to force a wait till termination:

    Dim p As New Process
    Dim psi As New ProcessStartInfo("program.exe", "parameters")
    Dim np As New Process
    np.StartInfo = psi
    np.Start()
    np.WaitForExit()
    Sadly, it didn't solve my problem, but I'm probably on the right track.


    Saturday, October 4, 2014 11:49 AM
  • Perhaps if you explain what program or cmd command you are using and showed how your using it we could help. The Process class also has an Exited event that can be used to notify your application when the Process that you start has Exited. There are also a few class libraries that have been designed by 3rd parties that may have this ability also.

     Here is an example of extracting a Zipped folder (.zip) that shows a system dialog with a progressbar that indicates the progress. It will not execute the next line of code until the Extracting is finished.

    ' You must add a refference to the Shell32.dll file lacated @ (C:\Windows\System32\Shell32.dll)
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ExtractFiles("C:\TestFolder\Unzipped", "C:\TestFolder\Zipped.zip")
    
            For Each f As String In IO.Directory.GetFiles("C:\TestFolder\Unzipped")
                ListBox1.Items.Add(f)
            Next
        End Sub
    
        Private Sub ExtractFiles(ByVal UnzippedDir As String, ByVal ZippedFile As String)
            If Not IO.Directory.Exists(UnzippedDir) Then IO.Directory.CreateDirectory(UnzippedDir)
            Dim ShellInterface As New Shell32.Shell()
            Dim OutputDir As Shell32.Folder = ShellInterface.NameSpace(UnzippedDir)
            Dim InputZip As Shell32.Folder = ShellInterface.NameSpace(ZippedFile)
            OutputDir.CopyHere(InputZip.Items)
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Saturday, October 4, 2014 12:11 PM
    Saturday, October 4, 2014 12:10 PM
  • I am using a third-party codec to extract a proprietary archive format known as "txx". Code is inside a button_click event:

    ' Disabling this block of code has no effect on the outcome.
    If FileIO.FileSystem.DirectoryExists("c:\OVC") Then
       FileIO.FileSystem.DeleteDirectory("c:\OVC", _
       FileIO.DeleteDirectoryOption.DeleteAllContents) ' Start fresh
    FileIO.FileSystem.CreateDirectory("c:\OVC")
    WriteFile() ' Writes data to the destination folder. ' Extract "Extra.p004.txx" so we can COPY and modify "VcVoiceSets.L=EN.bs". txx_codec("extract", strTKPath & "\Archives\2.114.001\Extra.p004.txx") ' Process is in a module. Creates "C:\OVC\temp\Extra.p004\Scripts\Shared\file.txt" Dim strTemp As String = "C:\OVC\temp\Extra.p004\Scripts\Shared" My.Computer.FileSystem.CurrentDirectory = strTemp ' CRASH! Error: "Could not find file 'C:\OVC\temp\Extra.p004\Scripts\Shared' My.Computer.FileSystem.CopyFile(strTemp, "C:\OVC\temp\Scripts\Shared\Voice\VcTemp.txt") ' Operate on a copy.

    The code for the "txx_codec()" process, which works fine, looks like this:

        Public Sub txx_codec(ByVal strDirection As String, ByVal strFile As String)
            Dim p As New Process
            If strDirection = "extract" Then
                Dim psi As New ProcessStartInfo(strInstallPath & "\txx_tools.exe", "-x -k 1 " & Chr(34) & strFile & Chr(34) & " " & "c:\OVC\temp")
                Dim np As New Process
                np.StartInfo = psi
                np.Start()
                np.WaitForExit() ' Wait for codec to finish
                ' adding a pause here doesn't help.
            End If
            If strDirection = "compress" Then
                System.Diagnostics.Process.Start(strInstallPath & "\txx_tools.exe", "-b -k 1 " & "c:\OVC\temp" & " " & Chr(34) & strFile & Chr(34))
            End If
        End Sub

    I tried manipulating the archive to remove the period from the path, but it made no difference, so that wasn't this issue. I thought I had the answer when I realized that the program was probably looking for the path before the extraction finished, but waiting for it didn't change anything.

    I have confirmed that the path does indeed exist when it claims it doesn't, even copied/pasted the path from Explorer.

    I'm totally stumped.

    ADDENDUM: Ah! Talking it out helped! Parsing the code line-by-line, I realized that strTemp inside my "CopyFile" didn't include the filename, only the folder. VS reports the error on the wrong line!)

    Saturday, October 4, 2014 1:48 PM