How to know how many event handlers are subscribed to a button's c

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a button click event, and would like to know the handlers listening to
this click event, so that no handler will be bound twice.

Any way?
 
None that I know of. The classes that subscribe to the event should manage
this. Is there some reason this is not a reasonable expectation?
 
Yes, this is possible (to some extent).

Could you define your event as follows:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
Delegate[] subscribedDelegates = MyEvent.GetInvocationList();
//can now recurse over subscribers
this.myEventHandler += value;
}
remove
{
this.myEventHandler -= value;
}
}

Please note I am not saying this is a good idea. I am just saying this is a
possible way it can be done.

HTH
Dan
 
I have a function, say, myFunction, will be called both locally and remotely
via a socket.

This function contains the statement "this.MyButton.Click+= MyHandler;". The
remote call to this function may happen variant times.

I can have a local variable to record the number of handlers listening for
this purpose. However the solution is not clean.


Dan Kelley said:
Yes, this is possible (to some extent).

Could you define your event as follows:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
Delegate[] subscribedDelegates = MyEvent.GetInvocationList();
//can now recurse over subscribers
this.myEventHandler += value;
}
remove
{
this.myEventHandler -= value;
}
}

Please note I am not saying this is a good idea. I am just saying this is a
possible way it can be done.

HTH
Dan

zhaounknown said:
I have a button click event, and would like to know the handlers listening to
this click event, so that no handler will be bound twice.

Any way?
 
Back
Top