Using State Machines
Implementing a state machine is a great way to keep your automated process under control and to be easily able to test and make changes.
Basically, you define all of the expected states of your process (such as waiting for server to respond) and capture events (such as response received) that cause a transition from state to state.
To plan this out, you can draw bubbles that represent each state and give them an enumeration value. Then draw arrows between each one that represent events that trigger the transition from state to state.
In code, you can implement a while loop, and inside this have a switch statement that evaluates the present state, and if certain conditions are met, change the state value.
So you can end up with simple, but robust code that is easy to debug and expand, unlike a bunch of nested if then else clauses.
For processes that take a long time to complete, consider running this as a background worker thread so that your app. doesn’t appear to freeze.
In the DOT NET framework look out for the background worker functionality, progress reporting, and completion events.
So using state machines is a nice way to code a multi-step process and know at all times where you are in the process.


Recent Comments