public CommandResult Execute(string fileName, string args) { lock (_executingLock) { if (isDisposed) { throw new ObjectDisposedException(nameof(HestExecutor)); } //记录开始时间 DateTime start = DateTime.Now; //开始进程 commandProcedure = BasicBooter.CommandProcedureManager.OpenCommand(fileName, args); commandProcedure.OutputReceived += OnOutputReceived; //触发事件 CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args)); var cmdResult = commandProcedure.Execute(); commandProcedure.Dispose(); //记录结束时间 DateTime end = DateTime.Now; //构造结果对象 var result = new CommandResult(cmdResult.ExitCode, cmdResult.Output); //触发结束事件 CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start)); //返回结果 return(result); }; }
/// <summary> /// 无锁的执行..不建议覆写 /// </summary> /// <param name="fileName"></param> /// <param name="args"></param> /// <returns></returns> protected virtual Result ExecuteWithoutLock(string fileName, string args) { if (disposedValue) { throw new ObjectDisposedException(nameof(CommandExecutor)); } int exitCode; ProcessStartInfo pStartInfo; outputBuilder.Clear(); pStartInfo = GetStartInfo(fileName, args); DateTime start = DateTime.Now; currentProcess = Process.Start(pStartInfo); OnProcessStarted(currentProcess); try { CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args)); ExecutingSource?.Invoke(this, new CommandExecutingEventArgs(fileName, args)); } catch { } currentProcess.WaitForExit(); DateTime end = DateTime.Now; exitCode = currentProcess.ExitCode; OnProcessExited(currentProcess); currentProcess = null; var result = new Result(outputBuilder.Result, exitCode); try { CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start)); ExecutedSource?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start)); } catch { } return result; }
private void FireExecutingEvent(Command command) { var eventArgs = new CommandExecutingEventArgs(command); CommandExecuting.Invoke(this, eventArgs); if (eventArgs.Cancel) { throw new CommandAbortedException("Command canceled by event handler"); } }
/// <summary> /// 执行命令 /// </summary> /// <param name="fileName"></param> /// <param name="args"></param> /// <returns></returns> public ICommandResult Execute(string fileName, string args) { lock (_executingLock) { if (isDisposed) { throw new ObjectDisposedException(nameof(HestExecutor)); } //输出构造器 outputBuilder.Clear(); //记录开始时间 DateTime start = DateTime.Now; //开始进程 currentProcess = Process.Start(GetStartInfo(fileName, args)); //监听 currentProcess.OutputDataReceived += (s, e) => { outputBuilder.AppendOut(e.Data); OutputReceived?.Invoke(this, new OutputReceivedEventArgs(e, false)); }; currentProcess.ErrorDataReceived += (s, e) => { outputBuilder.AppendError(e.Data); OutputReceived?.Invoke(this, new OutputReceivedEventArgs(e, true)); }; currentProcess.BeginOutputReadLine(); currentProcess.BeginErrorReadLine(); //触发事件 CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args)); //等待进程结束 currentProcess.WaitForExit(); currentProcess.CancelErrorRead(); currentProcess.CancelOutputRead(); //记录结束时间 DateTime end = DateTime.Now; //构造结果对象 var result = new HestExecutorResult() { ExitCode = currentProcess.ExitCode, Output = outputBuilder.Result }; //触发结束事件 CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start)); //返回结果 return(result); }; }
/// <summary>Executes the supplied command</summary> /// <param name="command">The command to execute.</param> /// <param name="context">The context passed to the command</param> public virtual void Execute(CommandBase <CommandContext> command, CommandContext context) { var args = new CommandProcessEventArgs { Command = command, Context = context }; if (CommandExecuting != null) { CommandExecuting.Invoke(this, args); } logger.Info(args.Command.Name + " processing " + args.Context); using (var tx = persister.Repository.BeginTransaction()) { try { args.Command.Process(args.Context); Utility.FindEmpty(args.Context.Content); tx.Commit(); if (CommandExecuted != null) { CommandExecuted.Invoke(this, args); } } catch (StopExecutionException) { tx.Rollback(); } catch (Exception ex) { tx.Rollback(); logger.Error(ex); throw; } finally { logger.Info(" -> " + args.Context); } } }
/// <summary> /// Raise the SMTP command as executing. /// </summary> /// <param name="command">The command that is executing.</param> internal void RaiseSmtpCommandExecuting(SmtpCommand command) { CommandExecuting?.Invoke(this, new SmtpCommandExecutingEventArgs(command)); }
/// <summary> /// The trigger function for <see cref="CommandExecuting"/> event. /// </summary> /// <param name="sqlCommand">The SQL command that is going for execution.</param> private void OnCommandExecuting(FbCommand sqlCommand, SqlStatementType statementType) { CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(sqlCommand, statementType)); }
/// <summary> /// Raise the command executing event. /// </summary> /// <param name="command">The command that is executing.</param> public void RaiseCommandExecuting(SmtpCommand command) { CommandExecuting?.Invoke(this, new SmtpCommandExecutingEventArgs(command)); }
/// <summary> /// Fires an event. /// </summary> /// <param name="command">The command.</param> /// <param name="connection">The connection.</param> protected void FireExecutingEvent(IDbCommand command, IDataConnection connection, IDbConnection conn) { CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(command, connection, conn)); }
protected virtual void OnCommandExecuting(string msg) { CommandExecuting?.Invoke(this, new CommandEventArgs { Message = msg }); }
protected virtual void OnCommandExecuting() { CommandExecuting?.Invoke(this, EventArgs.Empty); }