Exemple #1
0
 /// <summary>
 /// Writes a line to stdin. A redirector must have been provided that indicates not
 /// to close the StandardInput stream.
 /// </summary>
 /// <param name="line"></param>
 public void WriteInputLine(string line)
 {
     if (IsStarted && redirector != null && !redirector.CloseStandardInput())
     {
         process.StandardInput.WriteLine(line);
         process.StandardInput.Flush();
     }
 }
        private ProcessOutput(Process process, Redirector redirector)
        {
            _arguments  = QuoteSingleArgument(process.StartInfo.FileName) + " " + process.StartInfo.Arguments;
            _redirector = redirector;
            if (_redirector == null)
            {
                _output = new List <string>();
                _error  = new List <string>();
            }

            _process = process;
            if (_process.StartInfo.RedirectStandardOutput)
            {
                _process.OutputDataReceived += OnOutputDataReceived;
            }
            if (_process.StartInfo.RedirectStandardError)
            {
                _process.ErrorDataReceived += OnErrorDataReceived;
            }

            if (!_process.StartInfo.RedirectStandardOutput && !_process.StartInfo.RedirectStandardError)
            {
                // If we are receiving output events, we signal that the process
                // has exited when one of them receives null. Otherwise, we have
                // to listen for the Exited event.
                // If we just listen for the Exited event, we may receive it
                // before all the output has arrived.
                _process.Exited += OnExited;
            }
            _process.EnableRaisingEvents = true;

            try {
                _process.Start();
            } catch (Exception ex) {
                if (IsCriticalException(ex))
                {
                    throw;
                }
                if (_redirector != null)
                {
                    foreach (var line in SplitLines(ex.ToString()))
                    {
                        _redirector.WriteErrorLine(line);
                    }
                }
                else if (_error != null)
                {
                    _error.AddRange(SplitLines(ex.ToString()));
                }
                _process = null;
            }

            if (_process != null)
            {
                if (_process.StartInfo.RedirectStandardOutput)
                {
                    _process.BeginOutputReadLine();
                }
                if (_process.StartInfo.RedirectStandardError)
                {
                    _process.BeginErrorReadLine();
                }

                if (_process.StartInfo.RedirectStandardInput)
                {
                    // Close standard input so that we don't get stuck trying to read input from the user.
                    if (_redirector == null || (_redirector != null && _redirector.CloseStandardInput()))
                    {
                        try
                        {
                            _process.StandardInput.Close();
                        }
                        catch (InvalidOperationException)
                        {
                            // StandardInput not available
                        }
                    }
                }
            }
        }