title: Asynchronous capture from a process output not working properly
body: <p>I'm developing a control for a website, where user can upload his PDF, and after the upload a 3rd party CLI-tool launches to verify PDF against certain profile and generate a corresponding report.
  The tool mentioned is callas pdfToolbox 4 (available here <a href="http://www.callassoftware.com/callas/doku.php/en:download[" rel="nofollow noreferrer">http://www.callassoftware.com/callas/doku.php/en:download[</a>^])</p>
  
  <p>The problem is that on my control at the website I need to display in real-time progress bar of checking the PDF-file. All the AJAX-stuff for this functionality is already written (ajax-postbacks, updates of progress-bar, etc), but there's a problem with asynchronous updates from the process, which launches the pdf-checking tool.</p>
  
  <p>If you launch the tool from command-line window, you can see that, it generates output into standard output stream, which contains progress updates (in percents), as well as possible messages about errors in the PDF-file.</p>
  
  <p>However, if the tool is launched by the process which I create in my web-control, I don't receive the OutputDataReceived events until the check has been finished, and then many OutputDataReceived events come at once, one after one.</p>
  
  <p>My code is the following (I've written a small console-app to test things faster):</p>
  
  <pre><code>class Program
  {
  
          static string appString = "path-to-callas-cli";
          static string argString = "path-to-pdf-and-path-to-report-and-path-to-callas-profile";
  
           static void Main(string[] args)
          {
               ProcessStartInfo pInfo = new ProcessStartInfo(appString, argString);
              pInfo.UseShellExecute = false;
              pInfo.CreateNoWindow = true;
              pInfo.RedirectStandardOutput = true;
              pInfo.RedirectStandardError = true;
              pInfo.RedirectStandardInput = true;
              pInfo.ErrorDialog = true;
  
              Process process = new Process();
              process.StartInfo = pInfo;
              process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
              process.Exited += new EventHandler(process_Exited);
              process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
              process.Start();
  
              process.BeginOutputReadLine();
              process.BeginErrorReadLine();
  
              process.WaitForExit();
  
              Console.ReadKey();
          }
  
          static void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
          {
  
          }
  
          static void process_Exited(object sender, EventArgs e)
          {
          }
  
          static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
          {
              Console.WriteLine("Received async output: " + e.Data);
          }
  }
  </code></pre>
  
  <p>As I've said, all the output from Callas CLI comes at once in the end (while the check takes about 35 seconds). To test my own code, I've created a small console app, which outputs numbers 10-20-30-....-100 in 500 milli-second intervals, and it's output is displayed perfectly from the main app, coming with 500ms intervals.</p>
  
  <p>Any ideas?</p>