title: Receiving a delayed standard output message
body: <p>I'm trying to read the standard output of the following <a href="https://support.microsoft.com/en-us/kb/311272" rel="nofollow">devcon.exe</a> command in my application:</p>
  
  <pre><code>devcon rescan 
  </code></pre>
  
  <p>If I run it in cmd I get the following two lines:</p>
  
  <pre><code>Scanning for new hardware.
  Scanning completed.
  </code></pre>
  
  <p><strong>The first line is printed instantly, while the second one is printed with a small delay (which is almost certainly the cause of the problem).</strong></p>
  
  <p>I want to get this output in my application. I have the following method which should do just that, but instead only prints, as the received output, the first line (the output is stored in the 'message' variable). The <a href="https://msdn.microsoft.com/en-us/library/windows/hardware/ff544766(v=vs.85).aspx" rel="nofollow">return code</a> of the command I get is 2, which means it fails to execute correctly:</p>
  
  <pre><code>public Boolean rescanProgrammer()
      {
          Boolean rescanCompleted = false;
          String path_to_devcon = System.Environment.CurrentDirectory.ToString() + "\\devcon.exe";
          Boolean oneResultFound = false;
  
          if (File.Exists(path_to_devcon))
          {
              Process externalApp = new Process();
              externalApp.StartInfo.Verb = "runas"; // run as admin (only for vista and up)
              externalApp.StartInfo.FileName = path_to_devcon;
              externalApp.StartInfo.Arguments = "rescan"; 
              externalApp.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
              externalApp.StartInfo.UseShellExecute = false;
              externalApp.StartInfo.RedirectStandardOutput = true;
              externalApp.Start();
  
              List&lt;String&gt; message = new List&lt;String&gt;();
  
              using (StreamReader reader = externalApp.StandardOutput)
              {
                  while (!reader.EndOfStream)
                  {
                      string result = reader.ReadLine();
                      message.Add(result);
                      // Check for the line : "1 matching device(s) found."
                      // Usually it's the last line in the output
  
                      if (result.Contains("Scanning completed."))
                      {
                          oneResultFound = true;
                          break;
                      }
                  }
              }
  
              externalApp.WaitForExit();
              // Returned Devcon.exe exit code
              /*
               Return code    Meaning
                  0           Success
                  1           Requires reboot
                  2           Failure
                  3           Syntax error
              */
              MessageBox.Show(externalApp.ExitCode + "");
  
              // Message
              MessageBox.Show(String.Join("\n", message.ToArray()));
  
              //Check if the line was found
              if (oneResultFound == true)
              {
                  // We found the above line and the process exited, so we can
                  // safely return a value
                  rescanCompleted = true;
                  MessageBox.Show("\"Rescan completed\" line found!");
              }
              else
              {
                  MessageBox.Show("Rescan operation didn't complete successfully!");
              }
          }
          else
          {
              MessageBox.Show("Couldn't find Devcon.exe");
          }
          return rescanCompleted;
      }
  </code></pre>
  
  <p>Does somebody know, what do I have to change in order to get the full output? </p>
  
  <p>I've also tried using the <code>StreamReader.ReadToEnd()</code>, which also didn't work:</p>
  
  <pre><code>using (StreamReader reader = externalApp.StandardOutput)
  {
      String temp = reader.ReadToEnd();
      MessageBox.Show(temp);
  }
  </code></pre>
  
  <p>Does anybody have an idea how this could be solved? Thanks in advance.</p>