简体   繁体   中英

Appccelerate state machine state with timer

Ok I am sure I am not using the state machine correctly but here is subset of sample code. This is the Appccelerate.StateMachine which used to be bbvcommon.StateMachine.

fsm.In(State.Idle)
    .ExecuteOnEntry(() => { 
        // wake up and check if there are people still standing and if so restart
        if(currentlyTalkingTo.Count() > 0)
        {
            fsm.Fire(Event.PersonFound);
        }
    })
    .On(Event.PersonFound).Goto(State.WaitToRecognizePeople);

fsm.In(State.WaitToRecognizePeople)
    .ExecuteOnEntry(() => {
        Thread.Sleep(1000);
        fsm.Fire(Event.TimeOut);
    })
    .On(Event.TimeOut).Goto(State.Greet);

The issue is what the best way to handle a sleep? With this code calling fsm.Stop() when shutting the application down sometimes hangs the application. Commenting all the Thread.Sleeps() in the states fixes the issue so the application successfully shuts down.

What is the recommended way of handling states that need to time out and move to another state? Sample code would be appreciated.

The Thread.Sleep would block the state machine, both the PassiveStateMachine and ActiveStateMachine , so that you cannot react to other events anymore during the sleep time. That might be an explanation for why a call to Stop sometimes hangs the explanation.

I assume you don't just want to wait for a given period of time but want to wait for some event under a timeout condition. I would then recommend to start a Timer in the ExecuteOnEntry of WaitToRecognizePeople that, when elapsed, fires the event Event.Timeout .

If you have many states that have associated timeouts, to avoid a lot of duplicated code, you could even implement this in a separate class as a state machine extension, like I did in one of my projects using this library. (see http://www.appccelerate.com/statemachineextensions.html ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM