title: Run a commandline process and get the output while that process is still running?
body: <p>How I can run a commandline process and get the output while that process is still running?</p>
  
  <p>What I mean is to run a CLI process with its own progressbar, the executable itself takes a long time to finish the operation so I want to take that progress information from the own process to display the progress in my application, other way I don't have any info to display a progress until process finishes.</p>
  
  <p><strong>I'm working in a WindowsForm project, not a Console App.</strong></p>
  
  <p>I've tried to do the same using FFMPEG.exe (x64) and I can read the "progress" while FFMPEG is running, I can pick the progress from FFMPEG and do what I want, but with this executable I just can't do it and I don't know if is possible.</p>
  
  <p>The program is "dbPowerAmp CoreConverter", it's a music converter, I think the program sends all the outputs in Unicode encoding because to read the output I need to set the output encoding to Unicode.</p>
  
  <p>...Other problem is I can't find a way to read the StandardError output of this process even using Unicode so please if someone can help me with those two problems.</p>
  
  <p>Here is the application: <a href="http://www.dbpoweramp.com/install/dMC-R14.4-Ref-Trial.exe" rel="nofollow noreferrer">http://www.dbpoweramp.com/install/dMC-R14.4-Ref-Trial.exe</a></p>
  
  <p>Here is an example output of the program launched directly from CMD:</p>
  
  <p><img src="https://i.stack.imgur.com/m7MHI.png" alt="enter image description here"></p>
  
  <p>(What I need is to pick the progressbar " * " asterisk character amount while process is running to calculate and display that percentage also in my application)</p>
  
  <p>And here is my code:</p>
  
  <pre><code>Private Shared CoreConverter As New Process()
  
  Private Shared CoreConverter_Info As New ProcessStartInfo() With { _
                .CreateNoWindow = True, _
                .UseShellExecute = False, _
                .RedirectStandardOutput = True, _
                .RedirectStandardError = True _
  }
  
  Private Shared Sub Run_CoreConverter()
  
  
      ' Just for testing CMD Unicode output:
      '
      ' CoreConverter_Info.FileName = "cmd"
      ' CoreConverter_Info.Arguments = "/U /C C:\CoreConverter.exe " &amp; CoreConverter_Info.Arguments
  
  
      CoreConverter_Info.FileName = "C:\CoreConverter.exe"
      CoreConverter_Info.Arguments = String.Format("-infile=""{0}"" -outfile=""{1}"" -convert_to=""mp3 (Lame)""" ..., blah blah blah)
      CoreConverter_Info.StandardErrorEncoding = System.Text.Encoding.Unicode
      CoreConverter_Info.StandardOutputEncoding = System.Text.Encoding.Unicode
      CoreConverter.StartInfo = CoreConverter_Info
      CoreConverter.EnableRaisingEvents = True
      CoreConverter.Start()
      CoreConverter.WaitForExit()
  
  
      Dim readerStdOut As IO.StreamReader = CoreConverter.StandardOutput
  
      ' This part works with FFMPEG executable but not with Coreconverter.exe,
      ' What I mean is that the msgbox is displayed when CoreConverter.exe finishes :( 
      ' but with FFMPEG I can read the output while FFMPEG still running.
      While CoreConverter.StandardOutput.EndOfStream = True
          MsgBox(CoreConverter.StandardOutput.ReadLine)
      End While
  
      If CoreConverter.ExitCode &lt;&gt; 0 Then
          ' Throw New Exception(CoreConverter.StandardError.ReadToEnd)
  
          ' No way to read the ErrorOutput...
          MessageBox.Show(CoreConverter.StandardError.ReadToEnd, "CoreConverter", MessageBoxButtons.OK, MessageBoxIcon.Error)
  
          MessageBox.Show(CoreConverter.StandardOutput.ReadToEnd, "CoreConverter", MessageBoxButtons.OK, MessageBoxIcon.Error)
      End If
  
  End Sub
  </code></pre>
  
  <blockquote>
    <p>UPDATE:</p>
  </blockquote>
  
  <p>Really I'm so desesperated, all of my intents are epic fails, I've tried all what my skills can do (but that is not much).</p>
  
  <p>Even using multithread to run a process in a separated thread while I try to get the output from the main thread I can't try to retrieve the process output until the process exited, it's crazy!, I don't understand why this happens. </p>
  
  <p>Just is a crazy thing where I'm loosing a lot of hours by day, some people tell me that perhaps making a COM Object I can do it, well, I don't want to spent months learning how to make a COM Object just to pick a couple of characters from console, That months learning that would not help me with anything more in my life, simply as that, I need something more basic, maybe the idea of reading the buffer console could work, but sure I would not be able to read the buffer until the process exited, so.</p>
  
  <p>So. What can I do with this damn process?</p>
  
  <p>Just to make more than an update of my question to say my own desesperation words I will paste here a multi thread thing I've tried. </p>
  
  <p>...Like I've said I can't try to read the outputs while process still runing, and the error output just can't be readed (string always is empty) even when the process finishes (with an intentioned error ofcourse). </p>
  
  <pre><code>Public Shared error_output As String
  Public Shared standard_output As String
  Public Shared active As Boolean = False ' CoreConverter Thread is active?
  
  Public Shared MyThread As Threading.Thread = New Threading.Thread(AddressOf Run_CoreConverter)
  
  Public Shared Sub Convert_To_MP3(ByVal In_File As String, _
  
  ' blah blah blah
  'Run_CoreConverter()
  
      MyThread = New Threading.Thread(AddressOf Run_CoreConverter)
      ' MyThread.IsBackground = True
      active = True
      MyThread.Start()
      Get_Output()
  
  End Sub
  
  Public Shared Sub Get_Output()
  
  While active ' While Coreconverter.exe is runing...
  
      Try
          If Not CoreConverter.HasExited Then
              MsgBox(CoreConverter.StandardOutput.ReadToEnd) ' This will not be displayed until process has exited...
          End If
      Catch ex As Exception
  
      End Try
  
      If error_output IsNot Nothing Then
          MsgBox(error_output) ' This will not be displayed until process has exited...
      End If
  
      If standard_output IsNot Nothing Then
          MsgBox(standard_output) ' This will not be displayed until process has exited...
      End If
  
  End While
  
  MsgBox("end active")
  
  End Sub
  
  Public Shared Sub Run_CoreConverter()
      CoreConverter_Info.FileName = CoreConverter_Location
      CoreConverter_Info.StandardErrorEncoding = System.Text.Encoding.Unicode
      CoreConverter_Info.StandardOutputEncoding = System.Text.Encoding.Unicode
      CoreConverter.StartInfo = CoreConverter_Info
      CoreConverter.EnableRaisingEvents = False
  
      CoreConverter.Start()
      ' CoreConverter.WaitForExit()
  
      ' Threading.Thread.Sleep(2000)
      ' For x As Integer = 0 To 99999999
      error_output = CoreConverter.StandardError.ReadToEnd
      standard_output = CoreConverter.StandardOutput.ReadToEnd
      ' Next
  
      If CoreConverter.ExitCode &lt;&gt; 0 Then
          Throw New Exception(CoreConverter.StandardError.ReadToEnd)
           MessageBox.Show(CoreConverter.StandardError.ReadToEnd, "CoreConverter", MessageBoxButtons.OK, MessageBoxIcon.Error)
           MessageBox.Show(CoreConverter.StandardOutput.ReadToEnd, "CoreConverter", MessageBoxButtons.OK, MessageBoxIcon.Error)
      End If
  
      CoreConverter.Close()
      active = False
  
  End Sub
  </code></pre>