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

Stop/abort passive state machine #57

Open dmumladze opened 4 years ago

dmumladze commented 4 years ago

Question: Is there a way to abort the state machine to prevent any further transitions? I used the Stop() method, but it doesn't stop it. I understand it may not be recommended to do so, but there's no need to keep transitioning if there's an transient exceptions and whatnot... Could you please suggest a recommended way to deal with such cases?

ursenzler commented 4 years ago

Can you proved more information why stop didn't work? Was it an active or passive, an async or synchronous state machine? The behaviour of Stops is different for the 4 kinds of machines.

For exceptions, you can introduce a global transition (pack all states into a superstate that has this transition) for exceptions and fire an event representing this scenario with FirePriority so that it gets to the head of the queue of events to process.

dmumladze commented 4 years ago

It's AsyncPassiveStateMachine. As it transitions though states, I see TransitionExceptionThrown event triggering, but it keep going without stopping. I tried to call Stop in TransitionExceptionThrown handler, but state machine does not stop.

ursenzler commented 4 years ago

Yes, the passive state machines continue executing the current "stack" of events and will only stop once they are all executed. The state machine will pass exceptions to your code (via the event handler) and continue running. For me, that is the best the state machine can do. If you want to state machine to throw an exception, don0t register a listener for TransitionExceptionThrown. The state machine checks whether there is a listener.