/// <summary> /// Executes the macro routine /// </summary> /// <param name="macro"></param> /// <param name="register"></param> /// <param name="cancellationToken"></param> /// <param name="pauseToken"></param> private void DoExecute(IMacroRoutine macro, IMacroMethodRegister register, CancellationToken cancellationToken, IPauseToken pauseToken) { if (this._depth > 25) throw new StackOverflowException("Recursive depth of the macro exceeded the limit"); var context = new ExecutionContext(this, register, cancellationToken, pauseToken); // Increase the executor depth to prevent recursion this._depth++; for (var repetition = 1; repetition <= macro.Repetitions; repetition++) { // Fire repetition started event if this is the top level if (this._depth == 1) { this.MacroRoutine_RepetitionStarted(context, repetition); } // Execute the actions foreach (var action in macro.Actions) { // Cancel if requested if (cancellationToken.IsCancellationRequested) { return; } // Pause if requested while (pauseToken.PauseRequested && !cancellationToken.IsCancellationRequested) { Thread.Sleep(500); } // Fire action started event if (this._depth == 1) { this.MacroRoutine_OnActionStarted(context, action); } // Execute the action action.Execute(this._actionExecutor, context); // Fire action ended event if (this._depth == 1) { this.MacroRoutine_OnActionEnded(context, action); } } // Fire repetition ended event if (this._depth == 1) { this.MacroRoutine_RepetitionEnded(context, repetition); } } // Reduce the executor depth this._depth--; }
public SerializableMacro(IMacroRoutine routine, IEnumerable<IMacroMethod> register) { this.Repetitions = routine.Repetitions; this.Actions = routine.Actions.Select(a => (BaseAction) a).ToList(); this.Register = new List<SerializableMethod>(); foreach (var method in register) { this.Register.Add(new SerializableMethod(method.Name, method.Actions.Select(a => (BaseAction) a).ToList())); } }
/// <summary> /// Executes the macro routine async /// </summary> /// <param name="macro"></param> /// <param name="register"></param> /// <param name="cancellationToken"></param> /// <param name="pauseToken"></param> /// <returns></returns> public async Task ExecuteAsync(IMacroRoutine macro, IMacroMethodRegister register, CancellationToken cancellationToken = new CancellationToken(), IPauseToken pauseToken = null) { await Task.Factory.StartNew(() => this.DoExecute(macro, register, cancellationToken, pauseToken), cancellationToken); }
/// <summary> /// Executes the macro routine /// </summary> /// <param name="macro"></param> /// <param name="register"></param> /// <param name="cancellationToken"></param> /// <param name="pauseToken"></param> public void Execute(IMacroRoutine macro, IMacroMethodRegister register, CancellationToken cancellationToken = new CancellationToken(), IPauseToken pauseToken = null) { this.DoExecute(macro, register, cancellationToken, pauseToken); }