Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

looping state #605

Open
stefano-vardanega opened this issue Nov 21, 2024 · 0 comments
Open

looping state #605

stefano-vardanega opened this issue Nov 21, 2024 · 0 comments

Comments

@stefano-vardanega
Copy link

Hi, I have a state machine with 2 states: Idle and Pinging.
Pinging state permits reentry, to loop the ping forever.
The problem is that when I call _Machine.Fire(Triggers.Ping), the call does not exit.

Is there a better way to handle looping states?

Here a sample code:

// See https://aka.ms/new-console-template for more information
using Stateless;

var _MachineSynch = new StateMachine<State, Triggers>(State.Idle);
_MachineSynch.Configure(State.Idle)
                .Permit(Triggers.Ping, State.Pinging)
                .OnEntry(OnDisconnect);

_MachineSynch.Configure(State.Pinging)
    .PermitReentry(Triggers.Ping)
    .Permit(Triggers.Disconnect, State.Idle)
    .OnEntry(OnPing);

void OnDisconnect()
{
    Task.Delay(100).Wait();
    Console.WriteLine("Disconnected");
}


void OnPing()
{
    Console.WriteLine("Pinging...");
    Task.Delay(1000).Wait();
    Console.WriteLine("PingED. repinging");
    _MachineSynch.Fire(Triggers.Ping);
    Console.WriteLine("PingED. repinged");
}


//start synch
_MachineSynch.Fire(Triggers.Ping);
Console.WriteLine("FIRED"); //NEVER ARRIVES HERE

public enum State
{
    Idle,
    Pinging,
}

public enum Triggers
{
    //Connect,
    Ping,
    Disconnect,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant