Question: **Update:**

Again thanks for the examples, they have been very helpful and with the following, I don't mean
to take anything away from them.

Aren't the currently given examples, as far as I understand them & state-machines, only half of what we usually understand by a state-machine?  

In the sense that the examples do change state but that's only represented by changing the value of a variable (and allowing different value- changes in different states), while usually, a state machine should also change its behavior, and behavior not (only) in the sense of allowing different value changes for a variable depending on the state, but in the sense of allowing different methods to be executed for different states.

Or do I have a misconception of state machines and their common use?

---

**Original question:**

I found this discussion about [state machines & iterator blocks in c#](https://stackoverflow.com/questions/1406986/does-c-include-finite-state-machines) and tools to create state machines and whatnot for C#, so I found a lot of abstract stuff but as a noob, all of this is a little confusing.

So it would be great if someone could provide a C# source code-example that realizes a simple state machine with perhaps 3,4 states, just to get the gist of it.

Answer: 
You might want to use one of the existing open source Finite State Machines. E.g. bbv.Common.StateMachine found at <http://code.google.com/p/bbvcommon/wiki/StateMachine>. It has a very intuitive fluent syntax and a lot of features such as, enter/exit actions, transition actions, guards, hierarchical, passive implementation (executed on the thread of the caller) and active implementation (own thread on which the fsm runs, events are added to a queue).

Taking Juliets example the definition for the state machine gets very easy:

```
var fsm = new PassiveStateMachine<ProcessState, Command>();
fsm.In(ProcessState.Inactive)
   .On(Command.Exit).Goto(ProcessState.Terminated).Execute(SomeTransitionAction)
   .On(Command.Begin).Goto(ProcessState.Active);
fsm.In(ProcessState.Active)
   .ExecuteOnEntry(SomeEntryAction)
   .ExecuteOnExit(SomeExitAction)
   .On(Command.End).Goto(ProcessState.Inactive)
   .On(Command.Pause).Goto(ProcessState.Paused);
fsm.In(ProcessState.Paused)
   .On(Command.End).Goto(ProcessState.Inactive).OnlyIf(SomeGuard)
   .On(Command.Resume).Goto(ProcessState.Active);
fsm.Initialize(ProcessState.Inactive);
fsm.Start();

fsm.Fire(Command.Begin);

```

**Update**: The project location has moved to: <https://github.com/appccelerate/statemachine>
One more state machine for the list, mine: <https://github.com/IanMercer/Abodit.StateMachine>

In addition to simple states with entry and exit actions, and actions on each transition, this one is designed for use in async code. It also supports hierarchical states and compound state machines. So not really 'simple' but in use it's quite easy to code states and transitions.

```
static OpenClosedStateMachine()
{
    Closed
       .When(Fridge.eDoorOpens, (m, s, e, c) => Task.FromResult(Open));

    Open
        .When(Fridge.eDoorCloses, (m, s, e, c) => Task.FromResult(Closed));
}

```

Unlike others it also supports temporal transitions so it's easy to transition to a different state `After` a given period or `At` a given time.