private bool Cancel(bool wait, TimeSpan?timeout) { if (timeout < TimeSpan.Zero) { throw new ArgumentOutOfRangeException("timeout"); } OnExecuteContext ctx = this.CurrentContext; if (ctx == null) { return(false); } bool result = true; ctx.IsCancellationRequested = true; if (wait) { DateTimeOffset startTime = DateTimeOffset.Now; while (this.IsBusy) { if (timeout.HasValue == false) { // no timeout defined continue; } DateTimeOffset now = DateTimeOffset.Now; if ((now - startTime) >= timeout.Value) { // timeout reached => cancellation failed result = false; break; } } } this.IsCanceled = result; return(result); }
/// <summary> /// /// </summary> /// <see cref="IScriptExecutor.Execute(IEnumerable{char}, bool, bool)" /> public IScriptExecutionContext Execute(IEnumerable <char> src, bool autoStart, bool debug) { lock (this._SYNC) { this.ThrowIfDisposed(); ScriptExecutionContext result = new ScriptExecutionContext(); result.Executor = this; result.Source = StringHelper.AsString(src); result.IsDebug = debug; result.StartAction = delegate() { OnExecuteContext onExecCtx = new OnExecuteContext(); try { onExecCtx.IsDebug = result.IsDebug; onExecCtx.Source = result.Source; onExecCtx.StartTime = DateTimeOffset.Now; this.OnExecute(onExecCtx); } finally { result.Result = onExecCtx.ScriptResult; } }; if (autoStart) { result.Start(); } return(result); } }
/// <summary> /// The logic for the <see cref="ScriptExecutorBase.Execute(IEnumerable{char}, bool, bool)" /> method. /// </summary> /// <param name="context">The execution context.</param> protected abstract void OnExecute(OnExecuteContext context);
public void Start() { if (this.IsBusy) { throw new InvalidOperationException(); } FunctionExecutionContextCallback execCallback = null; string resultMsg = null; try { OnExecuteContext execCtx = new OnExecuteContext(); execCtx.InputParameters = this.InputParameters; execCtx.ResultCode = this.Function.DefaultResultCodeForSuccessfulExecution; execCtx.ResultMessage = StringHelper.AsString(this.Function.DefaultResultMessageForSuccessfulExecution); execCtx.ResultParameters = new Dictionary <string, object>(); this.CurrentContext = execCtx; this.IsCanceled = false; this.ResultCode = null; this.ResultMessage = null; this.ResultParameters = null; this.Errors = null; this.Function .OnExecute(execCtx); // result parameters IDictionary <string, object> paramsDict = this.Function.CreateEmptyParameterCollection(); foreach (KeyValuePair <string, object> item in execCtx.ResultParameters) { paramsDict.Add(item.Key ?? string.Empty, this.Function .ParseValueForParameter(item.Value)); } this.ResultParameters = new TMReadOnlyDictionary <string, object>(paramsDict); this.Errors = new Exception[0]; this.ResultCode = execCtx.ResultCode; resultMsg = (execCtx.ResultMessage ?? string.Empty).Trim(); execCallback = this.SucceededCallback; } catch (Exception ex) { execCallback = this.FailedCallback; this.ResultCode = this.Function.ResultCodeForUncaughtException; resultMsg = StringHelper.AsString(this.Function.GetResultMessageForException(ex)); AggregateException aggEx = ex as AggregateException; if (aggEx == null) { aggEx = new AggregateException(ex); } this.Errors = aggEx.InnerExceptions; } finally { if (StringHelper.IsNullOrWhiteSpace(resultMsg)) { resultMsg = null; } this.ResultMessage = resultMsg; this.CurrentContext = null; try { if (execCallback != null) { execCallback(this); } } finally { FunctionExecutionContextCallback cb = this.CompletedCallback; if (cb != null) { cb(this); } } } }