prevent duplicate event handlers

A

Arthur Dent

Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the
same event handler on an event?

e.g... I have some object which raises an event, and some other class which
consumes them. So in essence what I want to do is something like this in the
consumer class...

If Not objEventSource.SomeEvent.Handlers.Contains(AddressOf me.EventHandler)
Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler
 
M

Mattias Sjögren

If Not objEventSource.SomeEvent.Handlers.Contains(AddressOf me.EventHandler)
Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler

Can't you just keep a boolean the specifies whether or not the handler
has been added already?


Mattias
 
S

Stephany Young

I'm not convinced that your 'problem' is a problem.

My observations are that if you assign an event handler multiple times to
the SAME event (of the SAME instance of an object) then the AddHandler
keyword ignores the duplicates, viz:

AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler

When objEventSource.SomeEvent 'fires', me.EventHandler is only executed
once.

RemoveHandler objEventSource.SomeEvent, AddressOf me.EventHandler

When objEventSource.SomeEvent 'fires', me.EventHandler is no longer
executed at all.

This, in some respects is counter-intuitive as one could reasonably argue
that a delegate is still subscribed to the event, however this appears to
not be the case.

Try it and see if you observe the behaviour I described.

I think the implementation of the AddHandler keyword is different in C#
because you need to check if a delegated is subscribed before raising the
event.

Unfortunately the AddHandler keyword documentation neither confirms or
denies if this is the case or not.
 
S

Stephany Young

Sorry, belay that.

I was thinking of the situation of executing a RemoveHandler when there is
no delegate subscribed. In that case the RemoveHandler does not give any
feedback as to whether it succeeded or not.
 
P

Phill W.

Arthur said:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of
the same event handler on an event?

Short answer - you can't.

The delegate/event mechanism is /so/ fundamental to the .Net "event"
model that I have to ask - /why/ do you feel the need to do this?

Regards,
Phill W.
 
B

Brian Gideon

Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the
same event handler on an event?

e.g... I have some object which raises an event, and some other class which
consumes them. So in essence what I want to do is something like this in the
consumer class...

If Not objEventSource.SomeEvent.Handlers.Contains(AddressOf me.EventHandler)
Then _
AddHandler objEventSource.SomeEvent, AddressOf me.EventHandler

Have tried declaring an event using the Custom keyword?

Public Custom Event Foo As EventHandler

AddHandler(ByVal value As EventHandler)
' Add logic here to prevent duplicate handlers.
End AddHandler

RemoveHandler(ByVal value As EventHandler)

End RemoveHandler

RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

End RaiseEvent

End Event
 
A

Arthur Dent

Well, what I have is an event consumer class that is a strongly-typed
collection type. and it uses one event handler, dynamically assigned, to
listen to the same event of all the items contained in the collection - I
wanted to make sure that if the user added a second ref instance of an
object to the collection, it would not attach a second instance of the event
handler to that new instance.

I worked around it I guess, by simply adding code prohibiting them from
adding the same object to the collection more than once. It works anyway.

Stephany - attaching an event handler twice _will_ in fact, fire it twice. I
tested this by manually attaching a button click handler on a winform twice,
and putting a simple indicator in the onclick code. Clicking the button once
then runs the indicator code twice.
 
S

Stephany Young

See my earlier follow-up.


Arthur Dent said:
Well, what I have is an event consumer class that is a strongly-typed
collection type. and it uses one event handler, dynamically assigned, to
listen to the same event of all the items contained in the collection - I
wanted to make sure that if the user added a second ref instance of an
object to the collection, it would not attach a second instance of the
event handler to that new instance.

I worked around it I guess, by simply adding code prohibiting them from
adding the same object to the collection more than once. It works anyway.

Stephany - attaching an event handler twice _will_ in fact, fire it twice.
I tested this by manually attaching a button click handler on a winform
twice, and putting a simple indicator in the onclick code. Clicking the
button once then runs the indicator code twice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top