/// <summary> /// Uses a <see cref="CommandStartInfo"/> object to execute this command. /// </summary> /// <param name="startInfo">Specifies how to execute this command.</param> /// <returns>The exit code of the process.</returns> public int Execute(CommandStartInfo startInfo) { this.StartProcess(startInfo); this.process.WaitForExit(); return(this.CleanupProcess()); }
/// <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; }
public CommandStartingEventArgs(CommandStartInfo startInfo) { this.startInfo = startInfo; }
/// <summary> /// Executes the command with custom starting parameters. /// </summary> /// <param name="startInfo">The CommandStartInfo object that defines how the command should start</param> public void ExecuteAsync(CommandStartInfo startInfo) { this.process.Exited += this.ProcessExited; this.StartProcess(startInfo); }
public CommandStartingEventArgs(CommandStartInfo startInfo) { this.startInfo = startInfo; }
/// <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; }
/// <summary> /// Executes the command with custom starting parameters. /// </summary> /// <param name="startInfo">The CommandStartInfo object that defines how the command should start</param> public void ExecuteAsync(CommandStartInfo startInfo) { this.process.Exited += this.ProcessExited; this.StartProcess(startInfo); }
/// <summary> /// Uses a <see cref="CommandStartInfo"/> object to execute this command. /// </summary> /// <param name="startInfo">Specifies how to execute this command.</param> /// <returns>The exit code of the process.</returns> public int Execute(CommandStartInfo startInfo) { this.StartProcess(startInfo); this.process.WaitForExit(); return this.CleanupProcess(); }