Click here to Skip to main content
13,297,266 members (57,922 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 20 Jul 2004

C#/XML based state engine

, 20 Jul 2004
Rate this:
Please Sign up or sign in to vote.
How to create an XML based state engine for controlling a simple real-time system


Being tasked to look into ways of updating old C++ software, I needed to implement a simple state engine in C#. Wanting to create a generic solution that I can use throughout the code, I looked into having the actual state table written in XML along with a generic engine to process the table using reflection.

Using the code

The state table in XML simply defines a number of states and are identified by number. Each state has a function name and an associated action that is carried out when the function call is complete.

<?xml version="1.0" encoding="utf-8" ?> 
    <State Number="0">
        <Action Type="SLEEP">
    <State Number="10">
        <Action Type="MOVETONEXT">
    <State Number="20">
        <Action Type="MOVETONEXT">
    <State Number="30">
        <Action Type="EXIT" />
    <State Number="40">
        <Action Type="MOVETONEXT">

The state engine locates the function by using reflection and attaches it to the state's event. This is achieved by first getting state's event type and then attaching a delegate to it by specifying the target class (this.engineMethodsClass) instance and the name of the instance method (functionName).

// get our OnState event, then create and add a new delegate to the event
System.Reflection.EventInfo eventInfo = state.GetType().GetEvent("OnState");
System.Delegate eventDelegate = System.Delegate.CreateDelegate(
    eventInfo.EventHandlerType, this.engineMethodsClass, functionName);
eventInfo.AddEventHandler(state, eventDelegate);

An action is made up of the following enumeration values

  • SLEEP - causes the engine stay at the current state and to sleep for a period (in milliseconds)
  • MOVETONEXT - moves to the next state (indicated by the <NextState> tag). A sleep time can also be added here
  • EXIT - causes the engine to exit

To use the state engine first create a variable in the class that handles the state engine

private RQAssemblies.StateEngine.Engine engine = new 

In order to separate the events from the main application, you can specify the class where the events can be found. In this example we will simply point to our main application. We can then load the state table into the engine and instruct it to Run() .

// initialise and start up the engine 
engine.EngineMethodsClass = this;

The engine runs on its own thread for execution and therefore must be stopped when the application is terminated. Place the following code in the applications Close event

// instruct the state engine to stop execution
When the engine hits a state it invokes the attached event. In our example state table we need to define 5 events, where the prototype for the event is public void functionName(Action actionOnExit)
public void CheckInputs(RQAssemblies.StateEngine.Action actionOnExit)
    if((this.input1.Checked == true) && (this.input2.Checked == false))
        actionOnExit.Type = 
        actionOnExit.StateNumberToMoveTo = 10;
    else if((this.input1.Checked == false) && (this.input2.Checked == true))
        actionOnExit.Type = 
        actionOnExit.StateNumberToMoveTo = 20;
    else if((this.input1.Checked == true) && (this.input2.Checked == true))
        actionOnExit.Type = 
        actionOnExit.StateNumberToMoveTo = 30;
    else if((this.input1.Checked == false) && (this.input2.Checked == false))
        actionOnExit.Type = 
        actionOnExit.StateNumberToMoveTo = 40;

public void FirstPressed(RQAssemblies.StateEngine.Action actionOnExit)
    this.result.Text = "First Pressed";

public void SecondPressed(RQAssemblies.StateEngine.Action actionOnExit)
    this.result.Text = "Second Pressed";

public void BothPressed(RQAssemblies.StateEngine.Action actionOnExit)
    this.result.Text = "Both Pressed";

public void NonePressed(RQAssemblies.StateEngine.Action actionOnExit)
    this.result.Text = "None Pressed";

Within an event we can modify the exiting action of the state by modifying the actionOnExit parameter.

In our example state table, state 0 simply loops around and checks the states of 2 buttons. Depending on the button states we modify our exit action condition in order to branch to a different part of the state table.

Points of Interest

When I first started learning C#, I viewed reflection as a breath of fresh air from C++, but struggled to find a real-world/work-related application to try it out on. Now I've found one, I can see the doors that it has opened for me to significantly reduce my coding efforts in the future.


  • Version 1.0 - 20th July 2004 - Original


The implementation of the state engine is very basic and is here to simply show how you can leverage the benefits of XML and reflection in creating a real-time state engine. Clearly, this example isn't interfacing to a real-time system as I can't ZIP my barcode scanner along with this article. In the future I intend to modify this code to be more flexible in terms of its linking between states and providing external input states and possibly 'Yes/No' style state handling.

But in the meantime I hope you find something in this article useful and look forward to reading your comments.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Russ Quinn
Web Developer
Australia Australia
Web developer with 15 years of commercial and industrial experience in the software world.
Now working as a contractor through his own company See his blog here
Personal homepage

You may also be interested in...


Comments and Discussions

GeneralNice job! Pin
Marc Clifton21-Jul-04 10:01
protectorMarc Clifton21-Jul-04 10:01 
GeneralRe: Nice job! Pin
Russq21-Jul-04 22:57
memberRussq21-Jul-04 22:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 21 Jul 2004
Article Copyright 2004 by Russ Quinn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid