Handling Events In C#
Events provide a very powerful (andconvenient) means for interprocess communication. They are particularly useful when changes in the operating environment require immediate response from concerned software modules. When we click on a button in a dialog box, for instance, an event is generated that tells the underlying program to take appropriate action. However, it is not necessary that an event be raised only in the case of an external action like a mouse click. Events can also be raised as a result ofinternal operations.
One of the greatest advantages of communication through events is that the sender and the receiver(s) ofthe messages (events) are loosely coupled. That is to say, asender-receiver relationship is not static and such relationships canbe established dynamically. When we write software using modern languages , we need to use events extensively to establish communication links between different modules. This article provides an introduction to how to do this in the context of C#.
The mechanism for implementing event-based communication is very similar to the way we go about enlisting ourselves for receiving information from various sources in everydaylife. As an example of such an activity, let us consider the process ofsubscribing to a magazine. The two entities absolutely essential for this are a publisher of a magazine and a subscriber. Similarly, for events, we need to have a sender of the event and a receiver or listener.
In order to subscribe we must enlist ourselves as subscribers and provide an address in a predefined format so that wemay receive the magazine at that address. The publisher maintains a list of subscribers and, when the magazine is ready, a copy is sent toeach name on that list.
The sender of an event, too, maintains such a list and also specifies a format for enlisting an \”address\”. Thisformat is implemented through a delegate. A delegate is a template fora method that every subscribing class must implement in order to be included in the sender’s list. So the first step in implementing eventsis to declare a delegate as follows :
public delegate void EventNameEventHandler( object sender, EventNameEventArgs e);
where ‘EventName’ is the name of the event to be raised and the second argument (<eventname>EventArgse) specifies a class that encapsulates information about the event. Inour example of subscription, it correponds to the magazine. This class,which must derive from System.EventArgs, is created as follows :</eventname>
public class EventNameEventArgs : EventArgs
{
…
…
…
}
The <eventname>EventArgs class will includeone or more properties which the client (subscribing) classes can readto get the necessary information about the event. The two elementsdescribed so far are independent entities and are not to be incuded inthe class that is raising the event.</eventname>
Many delegates and corresponding ‘EventArgs’classes are already available in the .NET Framework class librarycovering standard events like mouse or key actions. If one of these canbe used by you then it is not necessary to define the two above elements. The procedure described here is the general one for implementing event functionality and will come in handy when you needto create your own special events.
The next step is to create the class that raises the event — in our example the publisher of the magazine. Thisclass must incorporate two elements : an event and a method for notifying all subscribers. The first is used to hold the names of theclient (subscriber) classes. The second element is like the mailing department of the publisher — it sends the description of the event (<eventname>EventArgs) to all registered subscribers. The event is declared as follows :</eventname>
public event EventNameEventHandler EventName;
Note that we only declare the event and do not create it. The method for notifying all registered clients is written thus :Continues…












No comments yet... Be the first to leave a reply!