protected virtual void OnCommandStarting(CommandStartingEventArgs e) { if (this.CommandStarting != null) { this.CommandStarting(this, e); } }
/// <summary> /// Starts the process with customized starting information /// </summary> /// <param name="startInfo">CommandStartInfo object that defines how this command should start</param> private void StartProcess(CommandStartInfo startInfo) { if (this.IsExecuting) { throw new CommandException("Cannot execute this command because it is already running."); } var commandStartingEventArgs = new CommandStartingEventArgs(startInfo); this.OnCommandStarting(commandStartingEventArgs); if (commandStartingEventArgs.Cancel) { return; } this.IsExecuting = true; if (this.HasExecuted) { throw new CommandException("This command has already been executed.", null, this); } if (startInfo == null) { throw new ArgumentNullException("startInfo"); } var commandSyntaxAttribute = this.GetCommandSyntaxAttribute(); SyntaxBuilder syntaxBuilder = new SyntaxBuilder(this); var arguments = syntaxBuilder.Arguments; ProcessStartInfo processStartInfo = startInfo.GetProcessStartInfo(syntaxBuilder.FileName); processStartInfo.Arguments = arguments; processStartInfo.RedirectStandardError = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.UseShellExecute = false; this.process.StartInfo = processStartInfo; this.process.ErrorDataReceived += (s, e) => { this.errorOutputBuilder.AppendLine(e.Data); this.OnErrorOutputWritten(e.Data); }; this.process.OutputDataReceived += (s, e) => { this.standardOutputBuilder.AppendLine(e.Data); this.OnStandardOutputWritten(e.Data); }; this.process.Start(); this.process.BeginErrorReadLine(); this.process.BeginOutputReadLine(); this.process.EnableRaisingEvents = true; }