2

I've got a working test state machine in a console app - 3 states and 5 events.

Problem: How to run in Windows Forms ie do I have a main loop which is running all the time looking at state..and if so where...if am using events ie btnPress.

The goal is that the app can be in a number of different states/screens and it needs to be solid, so using a state machine to enforce where we are, and that there are no edge cases unhandled.

Working console app code:

namespace StateMachineTest {
class Program {
    static void Main(string[] args) {
        var fsm = new FiniteStateMachine();
        while (true) {
            if (fsm.State == FiniteStateMachine.States.EnterVoucherCode) {
                Console.WriteLine("State: " + fsm.State);
                Console.WriteLine("Enter Voucher Code:");
                string voucherCode = Console.ReadLine();
                Console.WriteLine("voucher is " + voucherCode);
                Console.WriteLine();
                fsm.ProcessEvent(FiniteStateMachine.Events.PressNext);
            }

            if (fsm.State == FiniteStateMachine.States.EnterTotalSale) {
                Console.WriteLine("State: " + fsm.State);
                Console.WriteLine("Enter Total Sale or x to simulate back");
                string voucherSaleAmount = Console.ReadLine();
                if (voucherSaleAmount == "x")
                    fsm.ProcessEvent(FiniteStateMachine.Events.PressBackToVoucherCode);
                else {
                    Console.WriteLine("total sale is " + voucherSaleAmount);
                    Console.WriteLine();
                    fsm.ProcessEvent(FiniteStateMachine.Events.PressRedeem);
                }
            }

            if (fsm.State == FiniteStateMachine.States.ProcessVoucher) {
                Console.WriteLine("State: " + fsm.State);
                Console.WriteLine("Press 1 to fake a successful redeem:");
                Console.WriteLine("Press 2 to fake a fail redeem:");
                Console.WriteLine("Press 3 to do something stupid - press the Next Button which isn't allowed from this screen");
                Console.WriteLine();
                string result = Console.ReadLine();

                //EnterVoucherCode state
                if (result == "1")
                    fsm.ProcessEvent(FiniteStateMachine.Events.ProcessSuccess);
                if (result == "2")
                    fsm.ProcessEvent(FiniteStateMachine.Events.ProcessFail);
                if (result == "3")
                    fsm.ProcessEvent(FiniteStateMachine.Events.PressNext);
            }

            //how to handle async calls?
            //how to handle many many states.. matrix could get unwieldy
        }
    }
}

class FiniteStateMachine {
    //first state is the default for the system
    public enum States { EnterVoucherCode, EnterTotalSale, ProcessVoucher };
    public enum Events { PressNext, PressRedeem, ProcessSuccess, ProcessFail, PressBackToVoucherCode };
    public delegate void ActionThing();

    public States State { get; set; }

    private ActionThing[,] fsm;

    public FiniteStateMachine() {
        //array of action delegates
        fsm = new ActionThing[3, 5] { 
        //PressNext,     PressRedeem,            ProcessSuccess,      ProcessFail,      PressBackToVoucherCode
        {PressNext,      null,                   null,                null,             null},                          //EnterVoucherCode.... can pressnext
        {null,           PressRedeem,            null,                null,             PressBackToVoucherCode},        //EnterTotalSale... can pressRedeem or pressBackToVoucherCode
        {null,           null,                   ProcessSuccess,      ProcessFail,      null} };                        //moving from ProcessVoucher... can be a processSuccess or ProcessFail.. can't go back to redeem
    }
    public void ProcessEvent(Events theEvent) {
        try {
            var row = (int)State;
            var column = (int)theEvent;
            //call appropriate method via matrix.  So only way to change state is via matrix which defines what can and can't happen.
            fsm[row, column].Invoke();
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message); //possibly catch here to go to an error state? or if do nothing like here, then it will continue on in same state
        }
    }

    private void PressNext() { State = States.EnterTotalSale; }
    private void PressRedeem() { State = States.ProcessVoucher; }
    private void ProcessSuccess() { State = States.EnterVoucherCode; }
    private void ProcessFail() { State = States.EnterVoucherCode; }
    private void PressBackToVoucherCode() { State = States.EnterVoucherCode; }
}

}

Not working WinForms code:

    //goal is to get a fsm demo working with 3 states and 5 events.
//need number buttons, redeem and back to work.
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e) {
        SystemSettings.ScreenOrientation = ScreenOrientation.Angle90;

        var fsm = new FiniteStateMachine();
        while (true)
        {
            if (fsm.State == FiniteStateMachine.States.EnterVoucherCode)
            {
                //Console.WriteLine("State: " + fsm.State);

                //if next/redeem button is pressed
                //fsm.ProcessEvent(FiniteStateMachine.Events.PressNext);
            }

            if (fsm.State == FiniteStateMachine.States.EnterTotalSale)
            {
                Console.WriteLine("State: " + fsm.State);
                Console.WriteLine("Enter Total Sale or x to simulate back");
                string voucherSaleAmount = Console.ReadLine();
                if (voucherSaleAmount == "x")
                    fsm.ProcessEvent(FiniteStateMachine.Events.PressBackToVoucherCode);
                else
                {
                    Console.WriteLine("total sale is " + voucherSaleAmount);
                    Console.WriteLine();
                    fsm.ProcessEvent(FiniteStateMachine.Events.PressRedeem);
                }
            }

            if (fsm.State == FiniteStateMachine.States.ProcessVoucher)
            {
                Console.WriteLine("State: " + fsm.State);
                Console.WriteLine("Press 1 to fake a successful redeem:");
                Console.WriteLine("Press 2 to fake a fail redeem:");
                Console.WriteLine("Press 3 to do something stupid - press the Next Button which isn't allowed from this screen");
                Console.WriteLine();
                string result = Console.ReadLine();

                //EnterVoucherCode state
                if (result == "1")
                    fsm.ProcessEvent(FiniteStateMachine.Events.ProcessSuccess);
                if (result == "2")
                    fsm.ProcessEvent(FiniteStateMachine.Events.ProcessFail);
                if (result == "3")
                    fsm.ProcessEvent(FiniteStateMachine.Events.PressNext);
            }
        }
    }

    private void btn_0_MouseUp(object sender, MouseEventArgs e)
    {
            txtCode.Text += '0';
    }

    private void btn_1_MouseUp(object sender, MouseEventArgs e)
    {
            txtCode.Text += '1';
    }

    private void btn_2_MouseUp(object sender, MouseEventArgs e)
    {
            txtCode.Text += '2';
    }

    private void btn_del_MouseUp(object sender, MouseEventArgs e)
    {
            txtCode.Text = txtCode.Text.Substring(0, txtCode.Text.Length - 1);
    }

    private void btn_redeem_MouseUp(object sender, MouseEventArgs e)
    {
            txtCode.Visible = false;
            txtStatus.Visible = true;
            txtStatus.Text = "PROCESSING PLEASE WAIT";
    }

enter image description here

Code from: Simple state machine example in C#?

Community
  • 1
  • 1
Dave Mateer
  • 6,588
  • 15
  • 76
  • 125

2 Answers2

3

There is no need to have a polling event loop that's constantly checking the state, that's what a WinForm does automatically. You should have your UI elements wire up event handlers, and those event handlers should be responsible for checking/toggling state.

This is a very dirty implementation. If you apply the State Pattern (Chapter 9 of Head First Design Patterns has a really clean example), you should be able to use your Form as the Client that holds another object corresponding to the Context that is called by the event handlers of your UI elements.

  • Thanks to Visionary and Polity. I've implemented the state pattern as described in the Head First book here - http://www.programgood.net/2011/11/28/StateMachineAndGumballConsoleWinFormTests.aspx – Dave Mateer Nov 29 '11 at 03:58
  • There's some duplication in the form body methods that can probably be refactored to a private helper so as not to violate DRY, but overall that looks fine. – Visionary Software Solutions Nov 29 '11 at 20:10
1

Your code smells in many ways. First of all, winforms works using a single thread, therefore with your loop, you block the thread and hence the form. Secondly, you work with Console logic within your winforms app... did you even test this? Thirdly, you never set the statemachine to a different state. Are you intending to make the buttons set the next state?

A state machine should loop something like this.

public class StateManager
{
    public void Transition(IState state)
    {
        state.Transition(CurrentState, StateManager);
    }

    public IState CurrentState { get; private set; }

    public event EventHandler StateSwitched;
}

public class FirstState : IState
{
    private Form _form;

    public FirstState(Form form)
    {
        _form = form;
    }

    public void Transition(IState oldState, StateManager stateManager)
    {
        _form.Closing += (sender, e) =>
        {
            stateManager.Transition(new SecondState(_form));
        };
    }
}

public class SecondState : IState
{
...
}
Polity
  • 14,734
  • 2
  • 40
  • 40