public Stream RunAndReadOutput( FileInfo fileToRun, IEnumerable <string> arguments, Stream inputData) { if (inputData == null) { throw new ArgumentNullException(nameof(inputData)); } var processSettings = ProcessStartInfoFactory.GetStandardProcessSettings(fileToRun, arguments); processSettings.RedirectStandardInput = true; using (var process = new Process { StartInfo = processSettings }) { process.Start(); System.Threading.Tasks.Task.Delay(100).Wait(); //throws a "Pipe ended" error when trying to write to std right away. Waiting a bit before writing seems to solve the problem, but this could be problematic if the system is slower... //the writing operation gets blocked until someone reads the output - when both in and out are redirected. var outputReadTask = System.Threading.Tasks.Task.Run <MemoryStream>(() => GetStandardOutput(process)); WriteToStdIn(process, inputData); var outputStream = GetTaskResult(outputReadTask); ProcessExitCode(process); return(outputStream); } }
public Stream RunAndReadOutput( FileInfo fileToRun, IEnumerable <string> arguments) { var processSettings = ProcessStartInfoFactory.GetStandardProcessSettings(fileToRun, arguments); using (var process = new Process { StartInfo = processSettings }) { process.Start(); var outputStream = GetStandardOutput(process); ProcessExitCode(process); return(outputStream); } }