/// <summary> /// Safely read the streams of the process /// </summary> /// <returns>true if the process completed before the timeout or cancellation</returns> public override bool Read(int secondsBeforeTimeOut) { var outputReaderArgs = new ReaderArgs() { Proc = Process, Reader = Process.StandardOutput, Progress = Progress }; if (Process.StartInfo.RedirectStandardOutput) { _outputReader = new Thread(new ParameterizedThreadStart(ReadStream)); _outputReader.Start(outputReaderArgs); } var errorReaderArgs = new ReaderArgs() { Proc = Process, Reader = Process.StandardError, Progress = Progress }; if (Process.StartInfo.RedirectStandardError) { _errorReader = new Thread(new ParameterizedThreadStart(ReadStream)); _errorReader.Start(errorReaderArgs); } var end = DateTime.Now.AddSeconds(secondsBeforeTimeOut); //nb: at one point I (jh) tried adding !_process.HasExited, but that made things less stable. while (MoreToRead()) { if (Progress.CancelRequested) { return(false); } Thread.Sleep(100); if (secondsBeforeTimeOut > 0 && DateTime.Now > end) { if (_outputReader != null) { _outputReader.Abort(); } if (_errorReader != null) { _errorReader.Abort(); } return(false); } } // See http://www.wesay.org/issues/browse/WS-14948 // The output reader threads may exit slightly prior to the application closing. // So we wait for the exit to be confirmed. Process.WaitForExit(1000); StandardOutput = outputReaderArgs.Results; StandardError = errorReaderArgs.Results; return(true); }
/// <summary> /// Safely read the streams of the process /// </summary> /// <param name="process"></param> /// <param name="secondsBeforeTimeOut"></param> /// <returns>true if the process completed before the timeout or cancellation</returns> public override bool Read(int secondsBeforeTimeOut) { var outputReaderArgs = new ReaderArgs() {Proc = _process, Reader = _process.StandardOutput, Progress = _progress}; if (_process.StartInfo.RedirectStandardOutput) { _outputReader = new Thread(new ParameterizedThreadStart(ReadStream)); _outputReader.Start(outputReaderArgs); } var errorReaderArgs = new ReaderArgs() {Proc = _process, Reader = _process.StandardError, Progress = _progress}; if (_process.StartInfo.RedirectStandardError) { _errorReader = new Thread(new ParameterizedThreadStart(ReadStream)); _errorReader.Start(errorReaderArgs); } var end = DateTime.Now.AddSeconds(secondsBeforeTimeOut); //nb: at one point I (jh) tried adding !_process.HasExited, but that made things less stable. while (MoreToRead()) { if (_progress.CancelRequested) return false; Thread.Sleep(100); if (secondsBeforeTimeOut>0 && DateTime.Now > end) { if (_outputReader != null) _outputReader.Abort(); if (_errorReader != null) _errorReader.Abort(); return false; } } // See http://www.wesay.org/issues/browse/WS-14948 // The output reader threads may exit slightly prior to the application closing. // So we wait for the exit to be confirmed. _process.WaitForExit(1000); StandardOutput = outputReaderArgs.Results; StandardError = errorReaderArgs.Results; return true; }