Esempio n. 1
0
        public virtual bool Start(ProcessInfo info, BuildLogger buildLogger)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info",
                                                "The process information cannot be null (Nothing).");
            }

            if (_process != null)
            {
                if (_process.HasExited == false)
                {
                    return(false);
                }

                _process.Dispose();
                _process = null;
            }

            try
            {
                _process = new Process();

                ProcessStartInfo startInfo = _process.StartInfo;

                startInfo.CreateNoWindow         = info.CreateNoWindow;
                startInfo.FileName               = info.FileName;
                startInfo.WorkingDirectory       = info.WorkingDirectory;
                startInfo.RedirectStandardOutput = info.RedirectOutput;
                startInfo.RedirectStandardError  = info.RedirectError;
                startInfo.UseShellExecute        = info.UseShellExecute;
                startInfo.Arguments              = info.Arguments;

                _process.EnableRaisingEvents = true;

                _process.ErrorDataReceived += new DataReceivedEventHandler(
                    OnProcessErrorReceived);
                _process.OutputDataReceived += new DataReceivedEventHandler(
                    OnProcessOutputReceived);
                _process.Exited += new EventHandler(OnProcessExited);

                _processInfo   = info;
                _processLogger = buildLogger;

                // Now, start the process - there will still not be output till...
                _startTime = DateTime.Now;

                _process.Start();

                // if we get here, send an event to the caller
                if (this.ProcessStarted != null)
                {
                    ProcessStartedEventArgs args = new
                                                   ProcessStartedEventArgs(_startTime, info);
                    this.ProcessStarted(this, args);
                }

                // Start the asynchronous read of the output stream
                _process.BeginErrorReadLine();
                _process.BeginOutputReadLine();

                // We must wait for the process to complete, if required...
                if (info.WaitForExit)
                {
                    _process.WaitForExit();
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (_process != null)
                {
                    _process.Dispose();
                    _process = null;
                }
                if (buildLogger != null)
                {
                    buildLogger.WriteLine(ex, BuildLoggerLevel.Error);
                }

                return(false);
            }
        }
Esempio n. 2
0
 public virtual bool Start(ProcessInfo info)
 {
     return(this.Start(info, null));
 }