EventHandler Delegates

  • Thread starter Thread starter JMWilton
  • Start date Start date
J

JMWilton

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".
 
JMWilton said:
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?
 
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.
 
jmw said:
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?
 
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.
 
jmw said:
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.
 
Back
Top