Subscribing to an Event


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.




Microsoft Visual C# 2005 Step by Step
Microsoft® Visual C#® 2005 Step by Step (Step By Step (Microsoft))
ISBN: B002CKYPPM
EAN: N/A
Year: 2005
Pages: 183
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net