Previous Page
Next Page

Subscribing to an Event

Like delegates, events come ready-made with a += operator. You subscribe to an event by using this += operator. In the automated factory, the software controlling each machine can arrange for the shutdown methods to be called when the MachineOverheating event is raised like this:

TemperatureMonitor tempMonitor = new TemperatureMonitor();
...
tempMonitor.MachineOverheating += delegate { folder.StopFolding(0) };
tempMonitor.MachineOverheating += welder.FinishWelding;
tempMonitor.MachineOverheating += painter.PaintOff;

Notice that the syntax is the same as for adding a method to a delegate. You can even subscribe by using an anonymous method. When the tempMonitor.MachineOverheating event runs, it will call all the subscribing methods and shut the machines down.


Previous Page
Next Page