Раздел «Технологии программирования».ProgressBarFromConsoleOutput:
-- DmitryGolovitsin? - 03 Jun 2004

ProgressBarFromConsoleApplication

Problem

I need to do progressbar in my project LameShell for console application lame.exe. It was necessary to read the standart output.

Decision

I found the article on codeproject by Mike Mayer. It is quite nice! cool! Thanks a lot for. Please vote for it on codeproject. Before reading this article look through the "Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads."

Realisation

You can find realisation in following modules: AsyncOperation and ProcessCaller see at the and of the article. Now using class ProcessCaller, we can change some methods.
//To launch process
...
processCaller = new ProcessCaller();
processCaller.FileName = @"lame.exe";
processCaller.WorkingDirectory=@"";
// processCaller.Failed += no event handler for this one, yet.
processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.Completed += new EventHandler(processCompleted);
processCaller.Cancelled += new EventHandler(processCanceled);
...

Here we used some class.

   /// <summary>
   /// Delegate used by the events StdOutReceived and
   /// StdErrReceived...
   /// </summary>
   public delegate void DataReceivedHandler(object sender,
   DataReceivedEventArgs e);
   /// <summary>
   /// Event Args for above delegate
   /// </summary>
   public class DataReceivedEventArgs : EventArgs
   {
      /// <summary>
      /// The text that was received
      /// </summary>
      public string Text;
      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="text">
      /// The text that was received for this event to be triggered.
      /// </param>

      public DataReceivedEventArgs(string text)
      {
         Text = text;
      }
   }

And now we can create managing functions.

private void processCompleted(object sender, EventArgs e)
{
...
// Do some work when process completed
...
}

private void processCanceled(object sender, EventArgs e)
{
...
//Do some work when process cancelled
...
}

private void writeStreamInfo(object sender, DataReceivedEventArgs e)
{
   ...
   //Here we read output and error stream 
   //Use e.Text to get the line
   ...
}

Example in my source

The output of lame.exe for encoding files is like in the picture below.

//--          1         2         3         4         5         6         7
//-- 123456789012345678901234567890123456789012345678901234567890123456789012345
//--     Frame          |  CPU/estimated  |  time/estimated | play/CPU |   ETA
//--     50/   235( 21%)| 0:00:01/ 0:00:03| 0:00:00/ 0:00:00|    2.1412| 0:00:00

So to take, for example, progress percent for current encoding file I used the code below in my.

private void writeStreamInfo(object sender, DataReceivedEventArgs e)
      {
         ...   
         string percent=line.Substring(15,2);
         ...
      }

And then convert in format I need, so this remaining time. Total time and batch percent I calculate, using sizes of files to be encoded.

prgbar.PNG See the result cool!