My goal is to read output from a 7zip command line process in real time. I have coded an asynchronous output reader that uses BeginOutputReadLine. This method should return a new line immediately if it was send to output. Or, like MSDN says: When asynchronous read operations start, the event handler is called each time the associated Process writes a line of text to its StandardOutput stream.
This is my code
Private Sub StartProcess()
Dim Proc As New Process
Proc.StartInfo.FileName = Application.StartupPath & "\7z.exe"
Proc.StartInfo.WorkingDirectory = "D:\temp"
Proc.StartInfo.Arguments = "a -t7z ""D:\temp.7z"" -mx=9 -m0=LZMA -ms=on -mhc=on -mmt=on -mtc=off -mta=off -ptest -mhe=off"
Proc.StartInfo.UseShellExecute = False
Proc.StartInfo.RedirectStandardOutput = True
Proc.StartInfo.CreateNoWindow = True
AddHandler Proc.OutputDataReceived, AddressOf OutputHandler
Proc.Start()
Proc.BeginOutputReadLine()
Proc.WaitForExit()
Proc.Dispose()
Proc = Nothing
End Sub
Private Sub OutputHandler(sendingProcess As Object, outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
Console.WriteLine(outLine.Data)
End If
End Sub
The problem is that i do not get any line until the process ends. After that all output is returned to the associated stream OutputHandler. The result of my asynchronous code is very much the same as using the synchronous StandardOutput.ReadToEnd method. What am i doing wrong?
[edit] I have created a batch file for testing my code with a ping command. That seems to work! Does 7zip do something weird with it's output? Because it's looks like the problem has something to do with the output of 7zip rather than the code reading it.
Just remove Proc.WaitForExit() from your code - it causes your application to WAIT until it ends.
If you want to know when the process has exited then use another Addhandler to catch Proc.Exited event
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With