Skip to content

State Change Actions

scottctr edited this page Mar 29, 2018 · 1 revision

You may find a need to execute an action when you enter or exit a state. You can config this while setting up your state. Here's an example that records the time a telephone call starts when entering the Connected state and then end time when exiting the same state.

stateMachine.ConfigureState(TelephoneState.Connected)
  .AddEntryAction(telephone => telephone.StartTime = DateTime.Now)
  .AddExitAction(telephone => telephone.EndTime = DateTime.Now);

You can also execute an action when reentering a state. This action will be executed after any trigger occurs that doesn't cause the object to change to a new state. Here's an example:

stateMachine.ConfigureState(TelephoneState.Connected)
  .AddReentryAction(telephone => telephone.MissedCalls++);