public static void WaitForState <TActor, TState, TTrigger>(this IStatefulActor <TActor, TState, TTrigger> actor, TState desired, int timeout) where TState : struct { var completionSource = new System.Threading.Tasks.TaskCompletionSource <bool>(); void waitForUnstarted(TState oldState, StateMachine <TState, TTrigger> .Transition transition) { if (actor.InState(desired)) { completionSource.TrySetResult(true); } }; actor.StateChanged += waitForUnstarted; // Check for synchronous completion if (actor.InState(desired)) { completionSource.TrySetResult(true); } completionSource.Task.Wait(timeout); actor.StateChanged -= waitForUnstarted; }
/// <summary> Checks to see if the current states transition's conditions are satisfied /// in response to any state change in the given other actor, but only while in the /// current state (or optionally given super-state). If the other actor goes to any given /// fault state, an exception is thrown for this actor. </summary> protected void WatchOtherAndUpdate <OA, OS, OT>(IStatefulActor <OA, OS, OT> other, OS[] errorStates, TState?whileIn = null) where OS : struct => WatchOtherWhileInState ( other, (s, t) => true, () => { if (errorStates.Any(es => other.InState(es))) { throw new FrameworkWatchedStateException(other.GetType().Name + " unexpectedly went to state " + other.State, other); } else { UpdateStates(); } }, whileIn );