Implement custom events in .NET framework

Hello everyone,
In today’s post I would like to explain how to create .NET custom events. The .NET framework provides specific objects that enables simple creation and use of events.

The events allow to an object (or class) to let know other classes or other objects when a specific occurrence occurred. The class that generates and sends the event is called Publisher and the classes that are notified and manage the event are called Subscribers. An event can have multiple subscriber, and Subscribers can handle multiple events from different Publisher. Events that have no subscriber will never be recalled.

An ASP.NET application, or a Windows Forms application, usually subscribe to events generated by controls such as buttons, DialogBox and other input tools. The events are also very useful for threads synchronization.

Suppose that we want to implement our event so that, when a given occurrence is raised, the Publisher send a DataSet to all subscriber that are listening that event. The first step in the construction of an event is the definition of a delegate in a namespace.

1

public delegate void ProofEventHandler(object source, DataSet ds_example);

Delegates are reference types that derive from System.Delegate class. The application defines the address of a method to be invoked. When the notification happens the defined method will be called. The keyword “delegate” is used to declaring a method that is the prototype for the methods to be invoked. This is necessary because the return type and parameters of the .NET callback method must be defined during the compilation to avoid runtime errors.

We have now to implement an event in a Publisher class (using a Windows Form class containing a button “Button1″) that refers to the delegate previously created. The event will be raised when Button1 will be pressed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

public partial class PublClass : Form
{
public event ProofEventHandler proofEvent;
DataSet ds_orig;
...
...

public PublClass(){
ds_orig = new DataSet();
}

public void button1_Click(object sender, EventArgs e)
{
if (proofEvent != null)
{
proofEvent(this, ds_orig);
}
}

...
...

}

Now suppose we want to register a Subscriber class that will be notificated when proofEvent occurs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

public class SubscribClass
{
DataSet ds;

...
...

public SubscribClass(){
PublClass publisher = new PublClass();

//Event Registration
publisher.proofEvent += new ProofEventHandler(handle_proofEvent);
publisher.ShowDialog();
}

void handle_proofEvent(object source, DataSet ds_received)
{
ds = ds_received;
Console.WriteLine("Event catched!!!");
}

...
...

}

When Button1 will be pushed in the PublClass Form, the SubscribClass object will intercept the event, and then it will call the method “handle_proofEvent”. With this method the SubscribClass object acquires the DataSet sent by PublClass through proofEvent.