Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / Windows Forms
Article

Implementing an event which supports only a single event handler, in C#

Rate me:
Please Sign up or sign in to vote.
3.74/5 (6 votes)
30 Nov 20052 min read 62.3K   759   18   13
This article demonstrates how to implement an event, for which at a given point of time only one client can subscribe to. If multiple clients subscribe to the same event (of the same object), only the client subscribing last will get the event notification.

Sample Image - SinglecastEvent.jpg

Introduction

This article demonstrates how to implement an event which supports only a single listener, i.e., at a given point of time only one client can subscribe to that event. By default, for events, all the clients subscribing will get notified in a sequential order, but in this case, only the client subscribing last will get notified.

This type of an event is useful when we have multiple threads running which subscribe for a particular event notification, but it is enough that this notification is processed by only one thread. In the case of normal events, all the threads will get the notification and all threads will start processing the notification. In this case, the event will not be sent to all the threads, only the last subscribing thread will receive the notification. Hence, the notification processing method may not be thread safe (can avoid kernel objects for synchronization).

Background

In .NET, by default, events exhibit multicast behavior, i.e., an event notification is sent to all the clients who are subscribed to the event in the order in which they have subscribed. There will be a list of delegates subscribing to the event and notification is sent to all of them in an order. The problem here is if any one of the handlers throw an exception then the rest of the handlers will not get the notification, even if this exception is handled by the event raiser. Some times (as in the above case), we require an event which holds only one event handler, i.e., it won't pile up a list of handlers and exhibit a single-cast behavior.

Using the code

Run the SinglecastEvent.exe, and a form will be displayed which contains three buttons and a text box to display messages. Click on the "Client 1" button. Now, Client 1 is registered to an event which will be raised on clicking the "Raise event" button. The Client 1 message handler will display the message in the messages text box. Now, click on the "Client 2" button. On clicking the "Raise event" button again, the event will be handled only by the Client 2 message handler and Client 1 will not receive the notification.

The code contains a class EventRaiser which raises an event when the "Raise event" button is clicked. This class raises the "OnRaiseEvent" event and clients (forms) handle this event. The OnRaiseEvent is implemented as below:

C#
private EventHandler OnRaiseEventHandler;

public event EventHandler OnRaiseEvent 

{
    // Ensure that only one handler can
    // be subscribed to this event.

    add 
    {
        OnRaiseEventHandler = value; 
    }
    
    remove 
    {
        OnRaiseEventHandler -= value;
    }
}

//
//The following code represents the simulation of clients from the form.
//

private void btnClient1_Click(object sender, System.EventArgs e) { 
    txtMessages.Text += "Client 1 subscribed to event," + 
                        " other clients will stop listening to event. " + 
                        Environment.NewLine;
    // Simulate that client 1 is subscribing
    // to the OnRaiseEvent of EventRaiser class
    eventRaiser.OnRaiseEvent += 
           new EventHandler(Client1_OnRaiseEventHandler);
}

private void btnClient2_Click(object sender, System.EventArgs e) {
    txtMessages.Text += "Client 2 subscribed to event, " + 
                        "other clients will stop listening to event." + 
                        Environment.NewLine; 
    // Simulate that client 2 is subscribing
    // to the OnRaiseEvent of EventRaiser class
    eventRaiser.OnRaiseEvent += 
                new EventHandler(Client2_OnRaiseEventHandler);
}

private void Client1_OnRaiseEventHandler(object sender, EventArgs e)
{
    // This is client 1's handler for the event
    txtMessages.Text += "Client 1 : event received." + 
                        Environment.NewLine; 
}

private void Client2_OnRaiseEventHandler(object sender, EventArgs e) 
{
    // This is client 2's handler for the event
    txtMessages.Text += "Client 2 : event received." + 
                        Environment.NewLine; 
}

private void btnRaiseEvent_Click(object sender, System.EventArgs e) {
    eventRaiser.RaiseEvent();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I have been working in software industry for the past 5 years now. Enjoy working on complex and new technologies. Worked on Microsoft technologies Like VC++, COM, XML, SQL. Currently working on .Net, C#.

Comments and Discussions

 
Generalsimple, yet effective Pin
NormDroid1-Dec-05 0:13
professionalNormDroid1-Dec-05 0:13 
GeneralError dowloading source Pin
AxelM30-Nov-05 20:49
AxelM30-Nov-05 20:49 
GeneralRe: Error dowloading source Pin
Madhu Raykar1-Dec-05 18:54
Madhu Raykar1-Dec-05 18:54 
GeneralExcellent! Pin
Alexey A. Popov30-Nov-05 19:12
Alexey A. Popov30-Nov-05 19:12 
GeneralRe: Excellent! Pin
S. Senthil Kumar30-Nov-05 23:18
S. Senthil Kumar30-Nov-05 23:18 
GeneralRe: Excellent! Pin
Alexey A. Popov1-Dec-05 6:28
Alexey A. Popov1-Dec-05 6:28 
GeneralRe: Excellent! Pin
S. Senthil Kumar1-Dec-05 17:59
S. Senthil Kumar1-Dec-05 17:59 
GeneralRe: Excellent! Pin
Alexey A. Popov2-Dec-05 5:49
Alexey A. Popov2-Dec-05 5:49 
GeneralRe: Excellent! Pin
aviad_e21-Dec-05 21:37
aviad_e21-Dec-05 21:37 
GeneralRe: Excellent! Pin
Alexey A. Popov22-Dec-05 6:16
Alexey A. Popov22-Dec-05 6:16 
Well, googling never hurts Smile | :)

QuestionWhy not use a simple delegate? Pin
Robert Rohde30-Nov-05 17:45
Robert Rohde30-Nov-05 17:45 
AnswerRe: Why not use a simple delegate? Pin
Madhu Raykar30-Nov-05 17:56
Madhu Raykar30-Nov-05 17:56 
AnswerRe: Why not use a simple delegate? Pin
leppie30-Nov-05 18:21
leppie30-Nov-05 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.