public void ChangeState(MessageState newState) { MessageState currentState; if (!this.TryChangeState(newState, out currentState)) { throw new InvalidOperationException( string.Format("Invalid state change from {0} to {1}", currentState.Name, newState.Name)); } }
public bool TryChangeState(MessageState newState, out MessageState oldState) { var currentState = this.state; while (true) { if (!currentState.CanTransitionTo(newState)) { oldState = currentState; return false; } Interlocked.CompareExchange(ref this.state, newState, currentState); if (this.state == newState) { oldState = currentState; return true; } } }
public bool CanTransitionTo(MessageState newState) { return allowedTransitions.Contains(newState.Name); }
public MessageStateMachine(MessageState initialState) { this.state = initialState; }