Advertisement
Guest User

Untitled

a guest
Jun 4th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1.     psi.RedirectStandardError = true;
  2.         psi.RedirectStandardOutput = true;
  3.  
  4.         using (Process process = Process.Start(psi))
  5.         {
  6.           using (ManualResetEvent mreOut = new ManualResetEvent(false), mreErr = new ManualResetEvent(false))
  7.           {
  8.             try
  9.             {
  10.               process.OutputDataReceived += (o, e) =>
  11.               {
  12.                 //Console.WriteLine("Output Received: {0}", e.Data);
  13.                 if (e.Data == null)
  14.                   mreOut.Set();
  15.                 else if (output != null)
  16.                   output(e.Data);
  17.               };
  18.  
  19.               if (psi.RedirectStandardOutput)
  20.                 process.BeginOutputReadLine();
  21.  
  22.               process.ErrorDataReceived += (o, e) =>
  23.               {
  24.                 //Console.WriteLine("Error Received: {0}", e.Data);
  25.                 if (e.Data == null)
  26.                   mreErr.Set();
  27.                 else if (output != null)
  28.                   output(e.Data);
  29.               };
  30.  
  31.               if (psi.RedirectStandardError)
  32.                 process.BeginErrorReadLine();
  33.  
  34.               if (psi.RedirectStandardInput && input != null)
  35.               {
  36.                 string line;
  37.                 while (null != (line = input.ReadLine()))
  38.                   process.StandardInput.WriteLine(line);
  39.  
  40.                 process.StandardInput.Close();
  41.               }
  42.  
  43.               // Flush pipes
  44.               process.WaitForExit();
  45.  
  46.               if (psi.RedirectStandardOutput)
  47.                 mreOut.WaitOne();
  48.  
  49.               if (psi.RedirectStandardError)
  50.                 mreErr.WaitOne();
  51.  
  52.               exitCode = process.ExitCode;
  53.  
  54.               return process.ExitCode;
  55.             }
  56.             catch (Exception ex)
  57.             {
  58.               Logger.Error(ex.Message);
  59.               Logger.Error(ex.StackTrace);
  60.             }
  61.           }
  62.         }
  63.       }
  64.       finally
  65.       {
  66.         Logger.Log("Completed ExitStatus: {0}", exitCode);
  67.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement