473,466 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

EventHandler as method parameter

I found out that if you subscribe to or unsubscribe from an event in
the method that gets event handler as a parameter, it does not work as
expected.

If the event is declared as
event MyEventHandler myEventHandler;
you can subscribe to the event, using the construction
myEventHandler += subscriber;
but you cannot do the same by sending event handler as a parameter to
a method of the same class.
Construction
void AddSubscription(MyEventHandler myEH, MyEventHandler subscriber)
{
myEH += subscriber;
}
does not work, being called as
AddSubscription(myEventHandler, subscription);

"Does not work" means that subscription was successfully added to the
copy of the event invocation list, not to the original event.

At the same time, if AddSubscription() method has a slightly different
signature and event handler is sent by reference, it works:
void AddSubscription(ref MyEventHandler myEH, MyEventHandler
subscriber)
{
myEH += subscriber;
}
does work, being called as
AddSubscription(ref myEventHandler, subscription);

Some additional investigation shows that if you have the same two
event handler objects they are treated as equal until one of them is
changed.
MyEventHandler myEventHandler2 = myEventHandler;
bool b = myEventHandler2.Equals(myEventHandler); //true
myEventHandler2 += subscriber;
//at that moment myEventHandler2 has one more subscribers then
myEventHandler;
bool b = myEventHandler2.Equals(myEventHandler); //false

Since Delegate is not a Value Type, it looks as the system creates a
new object copy at the moment some changes are done (I could not find
any documentation about that).

It seems like by using ref, we found the workaround the problem.
However, if an event is thrown using BeginInvoke() and EndInvoke() as
it is for remote clients to support died client deletion, our
workaround does not work. BeginInvoke() uses AsyncCallback delegate as
its parameter. The only way to send the event handler to the callback
method is to set it as an object field and send the object as
AsyncState (BeginInvoke() last parameter). However, doing that we are
loosing the reference spec. So, I don't know how to overcome it.

The scenario when you want to use one set of methods for working with
a set of events could be very useful if your class has a plenty of
events each one of them has the same associated methods differ only by
event handlers' and EventArgs' names.

Is such a behavior of event handler a bug?
Nov 16 '05 #1
0 1299

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

Similar topics

2
by: Sandor Heese | last post by:
Question, When using BeginInvoke (on a From to marshal to the UI thread) with the EventHandler delegate I see some strange behaviour. The problem is that I call the UpdateTextBox method with a...
3
by: Remco | last post by:
Hello, Serverside I'm generating a html page. There are different controls for which I want to create an eventhandler manually. Like: document.forms.TextBox1.onchange = EventHandler;...
4
by: Torben | last post by:
For test purposes I attach an event to a control, say a TextBox TextChanged event. At another time the same event delegate is attached to some other control, maybe a listbox. Same event function...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: Rogerio Jun | last post by:
How I make this ? Item.Click += new EventHandler(methodname); methodname is a parameter. Rogerio
13
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
2
by: TPS | last post by:
I have some dynamic controls (LinkButton) that I am adding to my pages. When the link button is clicked, I am getting an ID from the LinkButton.CommandArguement value. I am then using that ID...
7
by: OfurGørn | last post by:
At runtime I generate 8 pictureboxes, when the user click on a picturebox I fire an onclick eventhandler. Because these pictureboxes are generate at runtime I do not have direct access to the their...
2
by: markliam | last post by:
I have auto-generated some code for a button by double clicking it. By default, the code is created with a return type of void and assigned to a click event. Now, I want the function to return a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.