appccelerate / statemachine

A .net library that lets you build state machines (hierarchical, async with fluent definition syntax and reporting capabilities).
Apache License 2.0
481 stars 128 forks source link

Issues Running StateMachine #41

Closed luckielordie closed 4 years ago

luckielordie commented 5 years ago

I'm having trouble getting the statemachine to transition.

Am I allowed to fire and event from inside an OnEntry function? When I've called Start on the statemachine do I need to do a while loop to keep the state machine open? Do you have a working code example for the tutorial on the site? It would be nice to see a full code structure just to see how you expect an IStateMachine to be interacted with.

ursenzler commented 5 years ago

Take a look at this test (https://github.com/appccelerate/statemachine/blob/master/source/Appccelerate.StateMachine.Specs/Async/Transitions.cs), it shows how the state machine can be used.

And here (http://www.appccelerate.com/statemachinesample.html) you find some explanations.

You can fire an event inside an OnEntry function, but I guess there would be a better state machine design that models the states better. A state that is left immediately is normally not a state, but part of a transition.

There are two flavours of the state machine: passive and active. The passive state machine executes a transition whenever you call Fire on it and will block the caller until it is finished. The active state machine has an internal thread that will execute the transition, the caller is not blocked. So you simply need to hold the instance of the state machine and make Firecalls (don't forget to first Initialize and then Start).

Let me know if this helps, otherwise I'll add a sample project to the solution.

luckielordie commented 5 years ago

Thanks for the swift reply! I'll have some time later on today to have a look at this.

I appreciate this is probably my naivety of the language, I'm seeing a lot of 'await' In the code sample that I have yet to learn fully. Thanks for your patience.

ursenzler commented 5 years ago

Yes, async/await is very verbose and needs getting used to.

ConfigureAwait(false) is needed to tell the framework to not switch back to the thread the call was originally from. Switching back normally only makes sense in UI or Web-Controller Code (.net full framework). On .net core, the framework never switches back, unless you have defined a SynchronizationContext which does.