473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

EventHandler Delegates

Is there a way to determine what has been added to the eventhandler list?
I have code that uses += and -= to turn event handling code on and off.
Apparently, it is possible to add the same event handler more than once.
Documentation refers to a Delegate invocationlist...but I can't seem to
access it from C#.
I am looking for a way to ensure that the contents of the eventhandler list
is "well known".
Nov 16 '05 #1
5 2761
JMWilton <JM******@comcast.net> wrote:
Is there a way to determine what has been added to the eventhandler list?
No. Or at least, not without using reflection and hoping that the event
hasn't been defined in a way other than the one C# happens to use by
default...
I have code that uses += and -= to turn event handling code on and off.
Apparently, it is possible to add the same event handler more than once.
Absolutely.
Documentation refers to a Delegate invocationlist...but I can't seem to
access it from C#.
I am looking for a way to ensure that the contents of the eventhandler list
is "well known".


One of the ideas of events is that contents of the eventhandler list
(along with where the eventhandlers are actually stored) is private.
All an event consumer needs to do is subscribe and later unsubscribe
from the event.

Could you tell us more about what you're trying to do?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
jmw
I want to turn off the BeforeCheck event handler when turning on/off
checkboxes in the child nodes....so that I do not fire and respond to a lot
of unnecessary events. Somewhere along the line...my code must be adding
the eventhandler twice and removing it once. I would like to be able to
clean out the invocation list so that I know precisely where I am at any
given point in time.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
JMWilton <JM******@comcast.net> wrote:
Is there a way to determine what has been added to the eventhandler list?

No. Or at least, not without using reflection and hoping that the event
hasn't been defined in a way other than the one C# happens to use by
default...
I have code that uses += and -= to turn event handling code on and off.
Apparently, it is possible to add the same event handler more than once.


Absolutely.
Documentation refers to a Delegate invocationlist...but I can't seem to
access it from C#.
I am looking for a way to ensure that the contents of the eventhandler

list is "well known".


One of the ideas of events is that contents of the eventhandler list
(along with where the eventhandlers are actually stored) is private.
All an event consumer needs to do is subscribe and later unsubscribe
from the event.

Could you tell us more about what you're trying to do?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
jmw <Jo***********@unisys.com> wrote:
I want to turn off the BeforeCheck event handler when turning on/off
checkboxes in the child nodes....so that I do not fire and respond to a lot
of unnecessary events. Somewhere along the line...my code must be adding
the eventhandler twice and removing it once. I would like to be able to
clean out the invocation list so that I know precisely where I am at any
given point in time.


You can't do that. You can, however, remove it before adding it, so you
never end up adding it twice in the first place.

Wouldn't it be better just to find out why you're adding it twice
though? You've been given an indication that you've got a bug: rather
than suppressing the symptom you can currently see, why not cure the
bug?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
jmw
Forcing the list to a know state would be part of the debugging effort to
isolate the misbehaving code. It was not intending to use it to mask the
problem.
Removing before adding seems to be just another way to mask the problem. It
appears that you can delete a handler that hasn't been added and not get an
error.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
jmw <Jo***********@unisys.com> wrote:
I want to turn off the BeforeCheck event handler when turning on/off
checkboxes in the child nodes....so that I do not fire and respond to a lot of unnecessary events. Somewhere along the line...my code must be adding the eventhandler twice and removing it once. I would like to be able to
clean out the invocation list so that I know precisely where I am at any
given point in time.


You can't do that. You can, however, remove it before adding it, so you
never end up adding it twice in the first place.

Wouldn't it be better just to find out why you're adding it twice
though? You've been given an indication that you've got a bug: rather
than suppressing the symptom you can currently see, why not cure the
bug?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
jmw <Jo***********@unisys.com> wrote:
Forcing the list to a know state would be part of the debugging effort to
isolate the misbehaving code. It was not intending to use it to mask the
problem.
Okay.
Removing before adding seems to be just another way to mask the problem.
Oh absolutely. I was suggesting it as a way of doing roughly what you
were asking for, before saying that it was a bad idea :)
It appears that you can delete a handler that hasn't been added and not get an
error.


Yup.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

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...
12
by: aplaxas | last post by:
Hi! "CDemo.Call" eventHandler is added to "button3.click" Event when both button1 and button2 are clicked. However, I want to add "cd.Call" only one time even though I clicked both button1 and...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
2
by: Wiktor Zychla | last post by:
Let's start with an example: if you have: class Test { public delegate int MyDelegate( int n ); event MyDelegate MyEvent; } then you can get MyEvent using reflection:
6
by: John B | last post by:
What is the reasoning for recommending that all events have the signature EventHandler(object sender, EventArgs e). Obviously I can create a custom delegate delegate void FooHandler(string foo)...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.