Help. Want to display running CLI process in real-time. Can only show result.

Mugsy's RapSheet 156 Reputation points
2021-08-15T12:16:28.817+00:00

I have a program that processes a collection of files, places them in a folder, and then compresses that folder using a CLI-based exe. Works fine.

Problem is, during the compression, despite redircting the process output to a textbox, I can't see what it is doing until it's done:

    Private Sub ZipFolder()
         Dim sOutput As String
         Dim oProcess As New Process
         ' Compress/Zip:
         With oProcess.StartInfo
             .FileName = Chr(34) & strLaunchDir & "\7za.exe" & Chr(34)
             .Arguments = "a -tzip " & Chr(34) & Path.GetDirectoryName(strCTKPath) & "\" & lblFolderName.Text & "\Folders\" & lblFolderName.Text & ".zip" & Chr(34) & " " & Chr(34) & Path.GetDirectoryName(strCTKPath) & "\" & lblFolderName.Text & "\Folders\" & lblFolderName.Text & Chr(34)  ' Command format: "7za.exe a [zip switch] "Dest" "Src"
             .UseShellExecute = False
             .RedirectStandardOutput = True
             .CreateNoWindow = True ' use this if you don`t want to see the cmd window.
         End With
         oProcess.Start()
         Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
             sOutput = oStreamReader.ReadToEnd()
             tbxOutput.AppendText(sOutput)     ' Append output w/o reassigning/printing the entire contents over & over. "Append" forces window to scroll.
             Application.DoEvents()  ' This gives labels time to update so it doesn't freeze during the process.
             tbxOutput.Refresh() ' Do this to update window in realtime.
         End Using
         oProcess.WaitForExit()
         oProcess.Close()
         oProcess.Dispose()
     End Sub

>

I thought "tbxOutput.Refresh()" would be enough, but clearly it isn't.

Is there a way to show the process output so I can see the compression taking place in real-time? TIA

UPDATE: I've learned the issue is related to "oStreamReader.ReadToEnd()", but I don't know how else to read/process an archive if not read in full first (or what other method to use instead... I assume in a loop... until [what]?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,361 Reputation points
    2021-08-16T02:57:32.843+00:00

    Hi @Mugsy's RapSheet ,

    Is there a way to show the process output so I can see the compression taking place in real-time?

    Take a look at the following code.

            ...  
            Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput  
      
                While Not oStreamReader.EndOfStream  
                    Dim line As String = oStreamReader.ReadLine()  
                    tbxOutput.AppendText(line)  
                    ...  
                End While  
                ...  
            End Using  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Mugsy's RapSheet 156 Reputation points
    2021-08-16T13:30:26.973+00:00

    Thanks for the reply!

    <strike>When you say "redirect the progress line", what line would that be? "-bsp" or "-bsp1" aren't valid 7za switches, so I'm not sure where to place it.</strike>
    Wait, still looking into it. "-bs" switch exists in "7z.exe" (not "7za.exe".)

    And is this using Xingyu's "oStreamReader.ReadLine()" or my original "oStreamReader.ReadToEnd()" method?

    TIA