title: UI Freeze using external Process
body: <p>I have implemented the following code (adapted from the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx" rel="nofollow">tutorial</a>) in order to run a command prompt window, run a program and read the output. The code is called from a ButtonClick event handler, which is nested in a User Control. </p>
  
  <p>I was under the impression that this would allow the rest of my program to function whilst the external process runs, due to the fact that the methods are 'asynchronous'. However, this does not appear to be the case, as my UI will freeze while the operation is running. I should add that the output received when the cmd process ends is correct.</p>
  
  <p>Sorry to dump a load of code like this, just not sure what else to do at this point!</p>
  
  <p>Any assistance would be greatly appreciated.</p>
  
  <pre><code>public static void runExternalProcess()
  {
      StringBuilder output = new StringBuilder();
  
      Process cmd = new Process();
      cmd.StartInfo.FileName = "cmd.exe";          
      cmd.StartInfo.UseShellExecute = false;
      cmd.StartInfo.CreateNoWindow = true;
      cmd.StartInfo.RedirectStandardOutput = true;
  
      cmd.OutputDataReceived += new DataReceivedEventHandler(outputEventHandler);        
      cmd.StartInfo.RedirectStandardInput = true;        
      cmd.Start();
      cmd.BeginOutputReadLine();      
  
      StreamWriter sortStreamWriter = cmd.StandardInput; 
      StreamWriter sw = cmd.StandardInput;
  
      if (sw.BaseStream.CanWrite)
      {
          sw.WriteLine("ping www.google.com");
      }
  
      sw.Close();
  
      cmd.WaitForExit();
  
      MessageBox.Show(output.ToString());
  
      cmd.Close();
  }
  
  private static void outputEventHandler(object sendingProcess, DataReceivedEventArgs e)
  {
      if (!String.IsNullOrEmpty(e.Data))
      {
          output.Append(e.Data + Environment.NewLine);
      }
  }
  </code></pre>