timer.Tick += new EventHandler(timer_Tick);
// Panel
Text = "Process Controller"; // caption
btnMessageOK = new Button();
btnMessageOK.Location = new Point(10,10);
btnMessageOK.Size = new Size(200,50);
btnMessageOK.Text = "MessageOK";
btnMessageOK.Click +=
new EventHandler(btnMessageOK_Click);
#region Other buttons (code not shown)
AutoScaleBaseSize = new Size(5, 13);
ClientSize = new Size(220, 250);
Controls.Add(btnMessageOK);
#region Add other buttons (code not shown)
}
// to be continued ...
Figure 3.5. Process control simulator application with events (1).
Overview 39
// ... continued
// Handlers
protected void timer_Tick(object sender, EventArgs e)
{
c.timer.Enabled = false; // Controller must restart timer
c.ReceiveEvent(ControlEvent.Timeout, null);
c.DispatchHandler();
}
#region Handlers for other buttons (code not shown)
protected void btnExit_Click(object sender, EventArgs e)
{
c.ReceiveEvent(ControlEvent.Exit, null);
Application.Exit();
}
// Run the simulation
public static void Main()
{
Application.Run(new Simulator());
}
}
}
Figure 3.6. Process control simulator application with events (2).
registers our method timer Tick (Figure 3.6) as a handler for this event. When the
timer times out, it raises its Tick event, which causes our handler method timer Tick
to execute. This machinery also ensures that events are delivered to our controller
one at a time, so each event is handled before the next is delivered.
Pages:
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76