1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Declaring an Event pdf

2 247 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 2
Dung lượng 7,93 KB

Nội dung

Declaring an Event You declare an event in a class intended to act as an event source. An event source is usually a class that monitors its environment, and raises an event when something significant happens. In the automated factory, an event source could be a class that monitors the temperature of each machine. The temperature monitor class would raise a “machine overheating” event if it detects that a machine has exceeded its thermal radiation boundary (i.e. it has become too hot). An event maintains a list of methods to call when it is raised. These methods are sometimes referred to as subscribers. These methods should be prepared to handle the “machine overheating” event and take the necessary corrective action: shut the machines down. You declare an event in a way similar to declaring a field. However, because events are intended be used with delegates, the type of an event must be a delegate, and you must prefix the declaration with the event keyword. For example, here's the StopMachineryDelegate delegate from the automated factory. I have relocated it to a new class called TemperatureMonitor, which provides an interface to the various electonic probes monitoring the temperature of the equipment (this is a more logical place for the event than the Controller class): class TemperatureMonitor { public delegate void StopMachineryDelegate(); . } You can define the MachineOverheating event, which will invoke the stopMachineryDelegate, like this: class TemperatureMonitor { public delegate void StopMachineryDelegate(); public event StopMachineryDelegate MachineOverheating; . } The logic (not shown) in the TemperatureMonitor class automatically raises the MachineOverheating event as necessary. An event maintains its own internal collection of attached delegates, so there is no need to manually maintain your delegate variable. . Declaring an Event You declare an event in a class intended to act as an event source. An event source is usually a class. class that monitors its environment, and raises an event when something significant happens. In the automated factory, an event source could be a class that

Ngày đăng: 24/12/2013, 09:16

TỪ KHÓA LIÊN QUAN

w