How do I read what is being displayed in a cli process that my program starts?

Recommended Answers

All 14 Replies

Elaborate your question please.

I need to be able to read what a program is outputting. So I want to read from the command prompt so I can put the stats to a progress bar. How can I do that?

Try

textBox1.Text = Console.In.ReadLine()

I finally got it to read the output, however, I need it to read it live. Basically, I want to have what it is displaying on my program, not just the end result. Is there a way to do that?

If you have control of the executing assemblies then just wrap your Console.Write() methods so you know what data is being displayed. If you don't then you will need to redirect stdout and stderr which can be done with the Process.

System.Diagnostics.Process.GetCurrentProcess().StandardOutput
System.Diagnostics.Process.GetCurrentProcess().StandardError

My program just hangs when I'm doing this. Here is my code, maybe you can fix it for me.

Dim myprocess As New Process
                    Dim StartInfo As New System.Diagnostics.ProcessStartInfo
                    StartInfo.FileName = "mp3hddecoder.exe" 'starts cmd window
                    StartInfo.Arguments = "-if """ & Input & """ -of """ & outputlocation & "wav"""
                    StartInfo.RedirectStandardInput = True
                    StartInfo.RedirectStandardOutput = True
                    StartInfo.RedirectStandardError = True
                    StartInfo.UseShellExecute = False 'required to redirect
                    StartInfo.CreateNoWindow = True
                    myprocess.StartInfo = StartInfo
                    myprocess.Start()
                    Dim SR As System.IO.StreamReader = myprocess.StandardError
                    Dim SO As System.IO.StreamReader = myprocess.StandardOutput
                    Do
                        SR = myprocess.StandardError
                        SO = myprocess.StandardOutput
                        Label7.Text = SO.ReadToEnd
                        Label5.Text = SR.ReadToEnd 'returns results of the command window
                    Loop Until myprocess.HasExited = True
                    SR.Close()
                    SO.Close()

You need to create another thread to monitor the streams for data changes and report it back to your UI thread.

How do I do that?

Can someone please help me. I can't figure out what to do.

Dim myprocess As New Process
        Dim StartInfo As New System.Diagnostics.ProcessStartInfo
        StartInfo.FileName = "mp3hddecoder.exe" 'starts cmd window
        StartInfo.Arguments = "-if """ & input & """ -of """ & outputlocation & "wav"""
        StartInfo.RedirectStandardInput = True
        StartInfo.RedirectStandardOutput = True
        StartInfo.RedirectStandardError = True
        StartInfo.UseShellExecute = False 'required to redirect
        StartInfo.CreateNoWindow = True
        myprocess.StartInfo = StartInfo
        myprocess.Start()
        Dim myThread As New Threading.Thread(AddressOf StartThread)
        myThread.Start(myprocess)

here the thread function inclusive the invoking of the labels. you have to invoke the labels when updating controls in a seperate thread. else it will "hang"

Private Sub StartThread(ByVal proc As Object)
        Dim myprocess As Process = DirectCast(proc, Process)
        Dim SR As System.IO.StreamReader = myprocess.StandardError
        Dim SO As System.IO.StreamReader = myprocess.StandardOutput
        Do
            SR = myprocess.StandardError
            SO = myprocess.StandardOutput
            Me.Invoke(New labelInvoke(AddressOf updateLabels), Label7, SO.ReadToEnd)
            Me.Invoke(New labelInvoke(AddressOf updateLabels), Label5, SR.ReadToEnd)
        Loop Until myprocess.HasExited = True
        SR.Close()
        SO.Close()
    End Sub

    Private Delegate Sub labelInvoke(ByVal lbl As Label, ByVal txt As String)

    Private Sub updateLabels(ByVal lbl As Label, ByVal txt As String)
        lbl.Text = txt
    End Sub

It isn't hanging anymore, but it isn't displaying anything in the labels and the files become locked.

yeah think your Do Loop is not correct. try this :

Private Sub StartThread(ByVal proc As Object)
        Dim myprocess As Process = DirectCast(proc, Process)
        Dim strLine As String = myprocess.StandardOutput.ReadLine()
        Do While strLine.Length > 0
            strLine = myprocess.StandardOutput.ReadLine()
            Me.Invoke(New labelInvoke(AddressOf updateLabels), Label5, strLine)
        Loop
    End Sub

this is now only for the standardoutput. the same way you can handle the error output.
the files are locked because you didnt kill the process after converting?

That didn't work. Any other ideas?

then you better attach your project so we can take a look into it (if possible with the mp3hddecoder.exe. there is might be another error.
did you even try to debug to see where it fails?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.