In event communication, the event sender class does not know which object or method will receive (process) the event it raises .. Net Framework defines a type (delegate) that provides mediation between events and event handlers. Technically, a delegate is a class that can save reference to a method. Unlike other classes, the delegate class has a signature and can only reference methods that match the signature. Although delegation has many other purposes, only the delegate event processing function is discussed here. The following example shows the event Delegate statement.
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
According to the Conventions, the event Delegate in. NET Framework has two parameters: the event source (represented by the sender parameter) and the event data (represented by the E parameter ).
You should call the protected oneventname method from the derived class (if this method is available) to raise the event. The oneventname method calls the delegate and transmits any event-specific data to trigger the event. The event Delegate method can perform event operations or process event-specific data.
The protected oneventname method also allows the derived class to override the event without attaching a delegate to it. The derived class must always call the oneventname method of the base class to ensure that the registered delegate receives the event. However, in a sealed or notinheritable class, you can directly trigger an event instead of indirectly triggering an event in the oneventname method.
Private alarmeventhandler m_alarmevent;
Public alarmeventhandler alarmevent
{
Add {This. m_alarmevent + = value ;}
Remove {This. m_alarmevent;-= value ;}
}
Public void onalarmevent
{
If (this. m_alarmevent! = NULL ){
This. (This. m_alarmevent (null, null)
}
}