Store added event handlers


A

Armin Zingler

Hi,

I add event handlers to different events of objects of different type. In an
array or arraylist, I want to store the information about which events I
added. Later, I want to process the arraylist and detach all the event
handlers. My problem is that I don't know which information to store. When
removing the handlers in a loop, the most important thing is that I don't
want to distinguish between the different object types and events, so I want
the code to be based on the basic Delegate/MulticastDelegate classes. I have
currently no clue how to do it. Something like:

dim o as new class1

addhandler o.ev1, addressof OnEv1
addhandler o.ev2, addressof OnEv2
...
for each i as Item in al 'al=arraylist, item=new class (see below)
removehandler i.???, i.???
next

Each time I use Addhandler I will create a new Item object and add it to the
arraylist. Though, I don't know what to store in an Item object. I thought
it must be a MulticastDelegate (the event) and a Delegate (the event
handler), but I am not able to store a reference to o.ev1 or o.ev2.

Is it possible at all or do I have to handle each event and object
individually? (btw, reflection is not an option) Thanks in advance.



Armin
 
Ad

Advertisements

A

Armin Zingler

Ok, I'm gonna rephrase:

- I have an object tree. This means I have a root object that contains a
list of sub objects, that can contain sub objects, and so on. All the
objects in the tree can raise events.

- In a Form_Load event, I recursively process the object tree and attach
event handlers to all the events of the objects in the tree.

- When an event fires, I detach the handler from the objects' events.

- When the Form closes, maybe not all events have been fired. Therefore,
there are still event handlers attached to these events.

My goal is:
I must detach the remaining event handlers when closing the Form. But how do
I know which event handlers are still attached? To solve it, I wanted to
remember them in a list. Whenever an event fires, I detach the event handler
and remove the corresponding entry from the list. So, if the Form closes, I
only have to process the list in order to detach the remaining handlers.

But, /what/ do I have to store in the list? I thought it's a) a
MultiCastDelegate object referencing the event and b) the Delegate that
points to the event handler. Using this information, I can call
Delegate.Remove on each item in the list. But, how can I get the
MultiCastDelegate object?

I hope, I made it clearer now.


Armin
 
L

Lloyd Sheen

Armin Zingler said:
Ok, I'm gonna rephrase:

- I have an object tree. This means I have a root object that contains a
list of sub objects, that can contain sub objects, and so on. All the
objects in the tree can raise events.

- In a Form_Load event, I recursively process the object tree and attach
event handlers to all the events of the objects in the tree.

- When an event fires, I detach the handler from the objects' events.

- When the Form closes, maybe not all events have been fired. Therefore,
there are still event handlers attached to these events.

My goal is:
I must detach the remaining event handlers when closing the Form. But how
do
I know which event handlers are still attached? To solve it, I wanted to
remember them in a list. Whenever an event fires, I detach the event
handler
and remove the corresponding entry from the list. So, if the Form closes,
I
only have to process the list in order to detach the remaining handlers.

But, /what/ do I have to store in the list? I thought it's a) a
MultiCastDelegate object referencing the event and b) the Delegate that
points to the event handler. Using this information, I can call
Delegate.Remove on each item in the list. But, how can I get the
MultiCastDelegate object?

I hope, I made it clearer now.


Armin
Armin,

To see what you are getting I have created a small class

Public Class Class1
Public Event Fart()

Public Sub ClickMe()
RaiseEvent Fart()
End Sub
End Class


Now to see what happens create a new windows app, add a button and in the
button click event add the following:

Dim f As Class1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim but As Button = DirectCast(sender, Button)
f = New Class1
f.ClickMe()
AddHandler f.Fart, AddressOf farthandler
f.ClickMe()
End Sub

Public Sub farthandler()

End Sub

You will notice if you watch the variable Fart in the class on the first
call to ClickMe it will be nothing. After the addhandler it will contain
the information I think you are looking for.

Hope this helps and sorry about the event name

Lloyd Sheen
 
A

Armin Zingler

Lloyd Sheen said:
Armin,

To see what you are getting I have created a small class

Public Class Class1
Public Event Fart()

Public Sub ClickMe()
RaiseEvent Fart()
End Sub
End Class


Now to see what happens create a new windows app, add a button and
in the button click event add the following:

Dim f As Class1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim but As Button = DirectCast(sender, Button)
f = New Class1
f.ClickMe()
AddHandler f.Fart, AddressOf farthandler
f.ClickMe()
End Sub

Public Sub farthandler()

End Sub

You will notice if you watch the variable Fart in the class on the
first call to ClickMe it will be nothing. After the addhandler it
will contain the information I think you are looking for.

Hope this helps and sorry about the event name
:) Thanks, but I don't see the relation to the question. My question was:
"But how do I know which event handlers are still attached?"
As I wrote, I attach many event handlers to handle events of many objects.
In order to remember which events I attached, I must store this informatino
in a list (Arraylist or whatever). I don't know how to do this.


Armin
 
Ad

Advertisements

A

Armin Zingler

Mudhead said:
Thanks for the link.

Meanwhile I found out that it is not possible what I was trying. The problem
is what's going on under the hood when adding/removing event handlers: The
AddHandler keyword doesn't expect an object as the first argument, but it's
the /name/ of an event. If it was an object, it would be possible to store a
reference. Everything done internally by executing Addhandler is specific to
the class and to the event. As it's specific, there is no multipurpose
solution that enables me storing references to events in the way intended.


Armin
 
Ad

Advertisements


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

Similar Threads

Adding an event handler in a sub 6
Adding keydown event handler 4
Adding an event handler for a socket object 1
Event Handlers 1
Event Handler 2
Event handlers 6
Event Handler 2
event handler 4

Top