forked from dotnet-state-machine/stateless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
41 lines (34 loc) · 1.44 KB
/
Program.cs
File metadata and controls
41 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using Stateless;
namespace OnOffExample
{
/// <summary>
/// This example has a simple state machine with only two states. The state
/// information is of type string, and the type of the trigger is char.
/// </summary>
class Program
{
static void Main(string[] args)
{
const string on = "On";
const string off = "Off";
const char space = ' ';
// Instantiate a new state machine in the 'off' state
var onOffSwitch = new StateMachine<string, char>(off);
// Configure state machine with the Configure method, supplying the state to be configured as a parameter
onOffSwitch.Configure(off).Permit(space, on);
onOffSwitch.Configure(on).Permit(space, off);
Console.WriteLine("Press <space> to toggle the switch. Any other key will exit the program.");
while (true)
{
Console.WriteLine("Switch is in state: " + onOffSwitch.State);
var pressed = Console.ReadKey(true).KeyChar;
// Check if user wants to exit
if (pressed != space) break;
// Use the Fire method with the trigger as payload to supply the state machine with an event.
// The state machine will react according to its configuration.
onOffSwitch.Fire(pressed);
}
}
}
}