954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading info from cli

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

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Elaborate your question please.

__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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?

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Try

textBox1.Text = Console.In.ReadLine()
__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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?

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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()
defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

How do I do that?

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
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
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

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

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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?

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

That didn't work. Any other ideas?

defiant91
Newbie Poster
9 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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?

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You