/// <summary> /// Creates a new instance of this state machine. /// </summary> public StateMachine(IStateMachineHost host) { _host = new WeakReference(host); // register for dispose StateMachinesDisposer.getInstance().RegisterSM(this); _CurrentState = null; _wes_PropertyChanged = new WeakEventSource <PropertyChangedEventArgs>(); _wes_StartTransition = new WeakEventSource <TransitionEventArgs>(); _wes_BeforeExitingPreviousState = new WeakEventSource <TransitionEventArgs>(); _wes_EndTransition = new WeakEventSource <TransitionEventArgs>(); this.Initialize(); _queue = new Queue <object>(); _timer = new System.Windows.Forms.Timer(); _timer.Interval = 1; _timer.Tick += _timer_Tick; _timer.Enabled = false; _effectsQueue = new Queue <EffectAction>(); _effectsTimer = new System.Windows.Forms.Timer(); _effectsTimer.Interval = 1; _effectsTimer.Tick += _effectsTimer_Tick;; _effectsTimer.Enabled = false; }
public ServiceCommunication(IStateMachineHost host, Uri?target, Uri type, InvokeId invokeId) { _host = host; _target = target; _type = type; _invokeId = invokeId; }
public StateMachineContext(StateMachine workflow, IStateMachineHost host, JToken?data, ObservableAction[]?targetActions, CancellationToken cancelToken) { workflow.CheckArgNull(nameof(workflow)); host.CheckArgNull(nameof(host)); this.Workflow = workflow; this.Host = new HostProxy(host); this.Data = data ?? new JObject(); this.CancelToken = cancelToken; _targetActions = targetActions; }
public static async Task <JToken> RunAsync(StateMachine workflow, IStateMachineHost host, JObject?input = null, ObservableAction[]?targetActions = null, CancellationToken cancelToken = default) { workflow.CheckArgNull(nameof(workflow)); host.CheckArgNull(nameof(host)); StateMachineContext?context = null; Func <CancellationToken, Task <JToken> > runTask = async token => { context = new StateMachineContext(workflow, host, input, targetActions, token); await context.RecordObservableActionAsync(ObservableAction.EnterStateMachine); try { return(await RunAsync(context)); } finally { await context.RecordObservableActionAsync(ObservableAction.ExitStateMachine); } }; JToken output; if (workflow.Timeout != null) { using var localTimeoutCancelTokenSource = new CancellationTokenSource(); using var combined = CancellationTokenSource.CreateLinkedTokenSource( localTimeoutCancelTokenSource.Token, cancelToken); Task <JToken> timeoutTask = host.DelayAsync(workflow.Timeout.Duration, combined.Token) .ContinueWith(_ => { return((JToken)JValue.CreateNull()); }); Debug.Assert(timeoutTask != null); output = await Task.WhenAny(timeoutTask, runTask(combined.Token)).Unwrap(); if (!timeoutTask.IsCompleted) { localTimeoutCancelTokenSource.Cancel(); } else if (workflow.Timeout.Action != null) { Debug.Assert(context != null); await workflow.Timeout.Action.ExecuteAsync(context, context.Data); } } else { output = await runTask(cancelToken); } Debug.Assert(output != null); return(output); }
public StateMachineHelper(IStateMachineHost h) { _host = h; }
public HostProxy(IStateMachineHost host) { _host = host; }