Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- psi.RedirectStandardError = true;
- psi.RedirectStandardOutput = true;
- using (Process process = Process.Start(psi))
- {
- using (ManualResetEvent mreOut = new ManualResetEvent(false), mreErr = new ManualResetEvent(false))
- {
- try
- {
- process.OutputDataReceived += (o, e) =>
- {
- //Console.WriteLine("Output Received: {0}", e.Data);
- if (e.Data == null)
- mreOut.Set();
- else if (output != null)
- output(e.Data);
- };
- if (psi.RedirectStandardOutput)
- process.BeginOutputReadLine();
- process.ErrorDataReceived += (o, e) =>
- {
- //Console.WriteLine("Error Received: {0}", e.Data);
- if (e.Data == null)
- mreErr.Set();
- else if (output != null)
- output(e.Data);
- };
- if (psi.RedirectStandardError)
- process.BeginErrorReadLine();
- if (psi.RedirectStandardInput && input != null)
- {
- string line;
- while (null != (line = input.ReadLine()))
- process.StandardInput.WriteLine(line);
- process.StandardInput.Close();
- }
- // Flush pipes
- process.WaitForExit();
- if (psi.RedirectStandardOutput)
- mreOut.WaitOne();
- if (psi.RedirectStandardError)
- mreErr.WaitOne();
- exitCode = process.ExitCode;
- return process.ExitCode;
- }
- catch (Exception ex)
- {
- Logger.Error(ex.Message);
- Logger.Error(ex.StackTrace);
- }
- }
- }
- }
- finally
- {
- Logger.Log("Completed ExitStatus: {0}", exitCode);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement