public ProcessHostRedirect(string commandLine = null) : base(commandLine) { LogCommandLine = true; TeeToConsole = false; PassEndOfStream = false; CommandLinePrefix = "COMMAND LINE: "; StdOutPrefix = ""; StdErrPrefix = "ERROR: "; OutputDataBuffer = new StringBuilder(); StartInfo.RedirectStandardOutput = StartInfo.RedirectStandardError = true; Process.OutputDataReceived += (sender, e) => { OutputDataHandlerRaw(OutputDataType.StdOut, e.Data); }; Process.ErrorDataReceived += (sender, e) => { OutputDataHandlerRaw(OutputDataType.StdErr, e.Data); }; OutputDataHandlerRaw = DefaultRawOutputDataHandler; OutputDataHandler = StringBuilderOutputDataHandler; }
public IEnumerable <OutputData> ExecuteToEnum(Action <ProcessHostRedirect> startAction = null) { PassEndOfStream = true; var outDataQueue = new Queue <OutputData>(); var enumOutDataReady = new AutoResetEvent(false); OutputDataHandler = (dataType, line) => { lock (outDataQueue) outDataQueue.Enqueue(new OutputData { Type = dataType, Line = line }); enumOutDataReady.Set(); }; ExecuteStart(); if (startAction != null) { startAction(this); } bool eofStdOut = false, eofStdErr = false; while (!eofStdOut || !eofStdErr) { enumOutDataReady.WaitOne(); while (true) { OutputData outData = null; lock (outDataQueue) { if (outDataQueue.Count > 0) { outData = outDataQueue.Dequeue(); } } if (outData == null) { break; } if (outData.Line == null) { if (outData.Type == OutputDataType.StdOut) { eofStdOut = true; } if (outData.Type == OutputDataType.StdErr) { eofStdErr = true; } } else { yield return(outData); } } } ExecuteWaitForExit(); }