Skip to content

looping state #605

@stefano-vardanega

Description

@stefano-vardanega

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,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions