public static int ExecuteProcess(string FileName, string CommandLine, string Input, out List<string> OutputLines) { using(ChildProcess NewProcess = new ChildProcess(FileName, CommandLine, Input)) { OutputLines = NewProcess.ReadAllLines(); return NewProcess.ExitCode; } }
public static int ExecuteProcess(string FileName, string CommandLine, string Input, out List <string> OutputLines) { using (ChildProcess NewProcess = new ChildProcess(FileName, CommandLine, Input)) { OutputLines = NewProcess.ReadAllLines(); return(NewProcess.ExitCode); } }
public static int ExecuteProcess(string FileName, string CommandLine, string Input, Action <string> OutputLine) { #if USE_NEW_PROCESS_JOBS using (ChildProcess NewProcess = new ChildProcess(FileName, CommandLine, Input)) { string Line; while (NewProcess.TryReadLine(out Line)) { OutputLine(Line); } return(NewProcess.ExitCode); } #else using (Process ChildProcess = new Process()) { try { object LockObject = new object(); DataReceivedEventHandler OutputHandler = (x, y) => { if (y.Data != null) { lock (LockObject){ OutputLine(y.Data.TrimEnd()); } } }; ChildProcess.StartInfo.FileName = FileName; ChildProcess.StartInfo.Arguments = String.IsNullOrEmpty(CommandLine) ? "" : CommandLine; ChildProcess.StartInfo.UseShellExecute = false; ChildProcess.StartInfo.RedirectStandardOutput = true; ChildProcess.StartInfo.RedirectStandardError = true; ChildProcess.OutputDataReceived += OutputHandler; ChildProcess.ErrorDataReceived += OutputHandler; ChildProcess.StartInfo.RedirectStandardInput = Input != null; ChildProcess.StartInfo.CreateNoWindow = true; ChildProcess.StartInfo.StandardOutputEncoding = new System.Text.UTF8Encoding(false, false); ChildProcess.Start(); ChildProcess.BeginOutputReadLine(); ChildProcess.BeginErrorReadLine(); if (!String.IsNullOrEmpty(Input)) { ChildProcess.StandardInput.WriteLine(Input); ChildProcess.StandardInput.Close(); } // Busy wait for the process to exit so we can get a ThreadAbortException if the thread is terminated. It won't wait until we enter managed code // again before it throws otherwise. for (;;) { if (ChildProcess.WaitForExit(20)) { ChildProcess.WaitForExit(); break; } } return(ChildProcess.ExitCode); } finally { CloseHandle(JobObject); } } #endif }
public static int ExecuteProcess(string FileName, string CommandLine, string Input, Action<string> OutputLine) { #if USE_NEW_PROCESS_JOBS using(ChildProcess NewProcess = new ChildProcess(FileName, CommandLine, Input)) { string Line; while(NewProcess.TryReadLine(out Line)) { OutputLine(Line); } return NewProcess.ExitCode; } #else using(Process ChildProcess = new Process()) { try { object LockObject = new object(); DataReceivedEventHandler OutputHandler = (x, y) => { if(y.Data != null){ lock(LockObject){ OutputLine(y.Data.TrimEnd()); } } }; ChildProcess.StartInfo.FileName = FileName; ChildProcess.StartInfo.Arguments = String.IsNullOrEmpty(CommandLine) ? "" : CommandLine; ChildProcess.StartInfo.UseShellExecute = false; ChildProcess.StartInfo.RedirectStandardOutput = true; ChildProcess.StartInfo.RedirectStandardError = true; ChildProcess.OutputDataReceived += OutputHandler; ChildProcess.ErrorDataReceived += OutputHandler; ChildProcess.StartInfo.RedirectStandardInput = Input != null; ChildProcess.StartInfo.CreateNoWindow = true; ChildProcess.StartInfo.StandardOutputEncoding = new System.Text.UTF8Encoding(false, false); ChildProcess.Start(); ChildProcess.BeginOutputReadLine(); ChildProcess.BeginErrorReadLine(); if(!String.IsNullOrEmpty(Input)) { ChildProcess.StandardInput.WriteLine(Input); ChildProcess.StandardInput.Close(); } // Busy wait for the process to exit so we can get a ThreadAbortException if the thread is terminated. It won't wait until we enter managed code // again before it throws otherwise. for(;;) { if(ChildProcess.WaitForExit(20)) { ChildProcess.WaitForExit(); break; } } return ChildProcess.ExitCode; } finally { CloseHandle(JobObject); } } #endif }