471,605 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Event and Event Handlers.

I'm trying to figure out if there is any way to determine if a class
has already registered as a handler for an event. Let's say I've got a
class Emitter that has an event
EmitterEvent
and a delegate
void EmitterEventHandler(Emitter sender)

I've got another class Listener that wants to listen for the
EmitterEvent. So I do the following:
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);

This, of course works, and if I do it twice:
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);

I recieve the event twice. I want to avoid that, so before registering
the EmitterEventHandler, I'd like to check if I'm already registered
for it. Something like this:

emitter.EmitterEvent.EmitterEvent.GetInvocationLis t().Contains(new
EmitterEventHandler(emitter_EmitterEvent);

Which is not possible.

Is there any way to do this? My current solution is to make the
EmitterEvents private and the following methods to my Emitter class:
public void RegisterHandler(EmitterEventHandler h)
{
//if EmitterEvent is null, just attach the Handler
if(this.EmitterEvent==null)
{
this.EmitterEvent+=h;
}
else
{
bool contains = false;
Delegate[] dels = EmitterEvent.GetInvocationList();
foreach(Delegate d in dels)
{
if(d == h)
{
contains = true;
break;
}
}
if(!contains)
{
this.EmitterEvent+=h;
}
}
}
and
public void UnRegisterHandler(EmitterEventHandler h)
{
this.EmitterEvent-=h;
}

This works, but isn't that pretty. Any better solutions?

Feb 9 '06 #1
2 1888
How about

emitter.EmitterEvent -= new EmitterEventHandler(emitter_EmitterEvent);
emitter.EmitterEvent += new EmitterEventHandler(emitter_EmitterEvent);

The first line will have no effect if the class has not already
subscribed to the event. The second line will always add the handler.
So, no matter what the situation, you always end up with the handler
exactly once on the event chain.

Feb 9 '06 #2
You're right. I was making this FAR to complicated. Although I think
I'm going to keep with the register/unregister idea. It seems a bit
cleaner.

Feb 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by tony kulik | last post: by
18 posts views Thread by bhavin | last post: by
13 posts views Thread by Charles Law | last post: by
6 posts views Thread by Kevin Attard | last post: by
14 posts views Thread by Hamed | last post: by
6 posts views Thread by Jake K | last post: by
6 posts views Thread by Tony Johansson | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.