Event Handling

  • Thread starter Thread starter GVN
  • Start date Start date
G

GVN

Hi All,

Can anyone explain me the lifecycle of event handling. How the .NET
framework handles

event handling?

Thanks,

GVN
 
Event Handling is done by the use of Delegates. A .Net delegate is similar
in many ways to the traditional function pointer. However, .Net delegates
are "multi-cast," meaning that each delegate can point to many methods.
These methods are stored in a linked list (of delegate instances) in the
delegate.

A .Net event is a delegate instance which may be assigned methods that match
the signature of the delegate, which is the Event Handler. Any method that
matches the signature of the delegate may be assigned to the delegate, via
adding it to the linked list. This is done using the C# += operator, or the
VB.Net AddHandler method.

The EventArgs class is the base class for all classes that are passed to
EventHandler delegates. These classes are used to pass data to all Event
Handlers that are subscribed to the event. This is by convention; a delegate
may actually take any signature desired, and delegates used in other
circumstances do.

Here is an example:

First, to create an event, you create a derivative of the EventArgs class,
which holds the data you wish to pass:

public class StatusEventArgs : EventArgs
{
private string _Message;
// The Message property is read-only in this case.
public string Message { get { return _Message; } }

// The _Message field is assigned in the constructor in this case.
public StatusEventArgs(string message)
{
_Message = message;
{
}

This is a simple derivative of EventArgs, which has a single member,
"Message." The name of the class follows the Microsoft naming convention for
EventArgs derivatives.

Now, we create a delegate:

public delegate void StatusEventHandler(object sender, string message);

Note that the delegate has nothing but a signature. The signature defines
what form the methods assigned to this delegate must follow. The name again
simply follows the Microsoft naming convention for Event Handler delegates.

Now, to use this in a class, you create an event field in the class:

public class foo
{
// Note that the keyword "event" identifies this as an EventHandler
delegate.
// The type specified (StatusEventHandler) is the delegate type that
must be used.
public event StatusEventHandler SendStatus;

// Conventionally, this delegate is invoked via a method which begins
with "On".
// Again, this is merely a convention. The delegate may be invoked
directly,
// as evidenced by the fact that the method defined below must do this.
// However, there are good reasons for following the convention
protected virtual void OnSendStatus(StatusEventArgs e)
{
// Must be sure that at least one method has been assigned to
// The delegate, in order to avoid a NullReferenceException
// Note that the signature of the method call follows the signature
of the delegate.
if (SendStatus != null) SendStatus(this, e);
}

// Here is an example of raising the event
// This is simply a method. It can be any process in the class.
public void DoSomething()
{
// do something

// We create an instance of StatusEventArgs, and pass it to the
OnSendStatus method.
OnSendStatus(new StatusEventArgs("Something has been done."));
}
}

Another class may then subscribe to the event:

public class bar
{
private foo theFoo;

// Here is our Event Handler method
// Note that the method signature matches the delegate signature.
private void foobar(object sender, StatusEventArgs e)
{
MessageBox.Show(e.Message);
}

public bar()
{
foo = new foo();

// The Event Handler is assigned by creating a delegate from the
// Event Handler method, and adding it to the foo class's
// SendStatus event delegate.
foo.SendStatus += new StatusEventHandler(foobar);
}
}

So, here's what happens during execution. When the instance of the foo class
has its "DoSomething" method invoked, the "DoSomething" method calls the
"OnSendStatus" method. This method, in turn, calls the foo class event
delegate. Each delegate assigned to the foo class event delegate is invoked
in turn, passing the instance of StatusEventArgs (which contains a message,
in this case). This causes the "foobar" method in the bar class to be
invoked, and the message is displayed in a MessageBox.

The C# -= operator may be used to unsubscribe from an event, as the VB
RemoveHandler method can.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.
 
Back
Top