473,837 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding out which objects have subscribed to a particular event ...

Hi

I want to find out what objects are due to receive an event i.e. those
that have added themselves as an event handler via +=. Yes, it's a
little pointless perhaps (or can anyone give some good uses for
this?!!).

How do I do this for an event on a class I implement? Also, how may I
do this for an event in the .Net framework e.g. a control?

This is for interest and knowledge so replies similar to "why do you
want to do this - it's not good practice because ..." are not
acceptable! :-)

Cheers

Emma Middlebrook
em************* *@fastmail.fm
Nov 15 '05 #1
1 2545
100
Hi Emma,

The answer of your question is: You might be able to find out the objects.
Basically events are not more then two accessor methods called *add* and
*remove* which are similar to the accessors for properties *get* and *set*.
The event MyEvent looks like

public event MyDelegate MyEvent
{
add
{
//Keep the reference to the delegate along with the references
already added
}
remove
{
//remove reference to the delegate
}

}

How the class will keep the references to delegates and call them when the
event should be fired is up to the implementer of the class.

Usually classes, which does not provide a lot of events use event keyword to
define an event:

<acces modifier> event MyDelegate MyEvent

The above line will be compiled by the c# compiler to something close to the
following lines:

private MyDelegate myEvent;

<acces modifier> event MyDelegate MyEvent
{
add
{
myEvent Delegate.Combin e(value, myEvent)
}
remove
{
myEvent = Delegate.Remove (myEvent, value)
}
}

So, you can see the Delegate reference, which holds the handlers list is
always private and no one from outside the class can access it in order to
traverse the list. Anyway inside the class list can be traversed.
This can be done in several ways. One of them is using
Delegate.GetInv ocationList, which returns an array of delegates. Each
delegate's Target property can be read in order to get the reference to the
subscribed object. This Property will be null if a static method has been
used.

However, if a class exposes a lot of events and the implementer expects a
few of them to be used at a time defining each event with the *event*
keyword is going to be a waste of memory (for each event a reference to a
delegate has to be allocated). Control class is good example of this. In
this case the delegates are kept in some kind of collection (usually
hashtable) and the delegate is added to the collection in the add accessor
if it is not already there.

So, You cannot find out the objects that have added themselves as an event
handler via += unless within the class itself. How can you do it depends on
the realization you have chosen.

I cannot say whether it is good or bad practice. If you need it of course
you can do it for your classes. Anyway you cannot do this for the class,
which you have not written :)

I'm not talking about using reflection to access private fields. Anyway, in
most of the cases there is no way for you to know how the delegate chains
are kept.

HTH
B\rgds
100

"emma middlebrook" <em************ **@fastmail.fm> wrote in message
news:e2******** *************** ***@posting.goo gle.com...
Hi

I want to find out what objects are due to receive an event i.e. those
that have added themselves as an event handler via +=. Yes, it's a
little pointless perhaps (or can anyone give some good uses for
this?!!).

How do I do this for an event on a class I implement? Also, how may I
do this for an event in the .Net framework e.g. a control?

This is for interest and knowledge so replies similar to "why do you
want to do this - it's not good practice because ..." are not
acceptable! :-)

Cheers

Emma Middlebrook
em************* *@fastmail.fm

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1326
by: Flack | last post by:
Hello, Is it possible to find out how many methods are listening to a certain event? For example, if a number of methods subscribed to a controls DragDrop event using +=, can I find out how many methods in total are listening to the DragDrop event of the control? If I can't find out the exact number of methods listening to the DragDrop event, can I somehow remove all listeners of that event at once? I tried using m_Control.DragDrop =...
1
1697
by: Flack | last post by:
Hello, Is it possible to find out how many methods are listening to a certain event? For example, if a number of methods subscribed to a controls DragDrop event using +=, can I find out how many methods in total are listening to the DragDrop event of the control? If I can't find out the exact number of methods listening to the DragDrop event, can I somehow remove all listeners of that event at once? I tried using m_Control.DragDrop =...
4
3150
by: Tedb | last post by:
Is there any reason why you can't reuse a delegate object instance versus creating a new one each time? For example in the following scenario I have a DataPoints object with an array of DataPoint objects, each of these has a Changed event. My Client objects subscribe to some or all of the DataPoint Changed events. Client1 creates a new delegate instance for each DataPoint event subscribed to, while Client2 creates one delegate instance...
0
2171
by: U S Contractors Offering Service A Non-profit | last post by:
This Sunday the 26th 2006 there will be Music @ Tue Nov Inbox Reply Craig Somerford to me show details 9:54 pm (26 minutes ago) #1St "CLICK" HeAt frOm A blanket --http://mail.google.com/mail/?realattid=f_3n2tmv&attid=0.1&disp=inline&view=att&th=10f0d56e8cb478be 24 oct 2006
9
2465
by: stevewy | last post by:
I am trying to write a function that will test all the checkboxes in a particular group of a form (it's a questionnaire), see whether more than three of them are ticked, and display a message if there are. To avoid putting an onClick on each individual checkbox, I am trying to put an onClick in the <TABLEtag that surrounds each question and use srcElement to tell which group has been clicked on and check each checkbox within that group in...
2
3060
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton / Server activated mode on startup. The Remoting Client accesses the Remoting Class through the interface of the Class and subscribes to an event of the Remoting Class that will be fired upon the private member value change.
3
2279
by: Cartoper | last post by:
My application appears to have a recourse leak. When the user starts a background process, the handle count in Process Explorer (PE) goes up by about 10, sometime 1 or 2 more, sometimes 1 or 2 less. When the task is completed, there are somewhere between 4 and 7 handles still open. My process is loading images and shrinking them down. I went through all the code and found some objects that implement the IDisposable interface which I...
275
12490
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
1953
tlhintoq
by: tlhintoq | last post by:
I'm pretty sure this is language independent and is going to be the same whether it's VC or C# - but my project is C# WIndows Forms just in case. Does anyone have a good handle on the sequence of events when an event is raised and multiple classes are all subscribed to the event? Does each class get a copy of the argument? Does each class get a pointer/address/ref to the one and only argument? Do all the classes get the argument at...
0
9846
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10897
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7823
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7009
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5859
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4056
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.