473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problems with static events? (Or is there another way?)

In a program I'm writing, I have a class that generates a certain
event. However, there may be dozens or even hundreds of instances
of this particular class, which makes adding and removing handlers
to each instance a pain. Rather than go down that route, I was
thinking of making the event static, so I would only need to add my
handlers once, to the static event, which I can then trigger from
inside my class.

That leaves me with two questions:

1. Is there a better way to handle this than using static events?

(I thought of having another class own a "master" event, but that
simply introduces more connecting code, and is a bit messier when
it comes to encapsulation.)

2. If this IS the best way, are there any "gotchas" or problems
I should be aware of when using static events?
Thanks in advance!

Colin
Nov 16 '05 #1
8 12500
You have quite a few options, with a static event probably being the
best option. Having static firing code is probably also a good action
to take, since it serializes your eventing code.

The only "gotcha" you are going to run into is that you can only have
a single *collection* of your class. Since you are using a static, if
any of your classes fires the event, then whomever is listening will
hear it. This means you can't provide any form of partitioning. An
example might be:

Say you have 10 people on a stock trading floor. All 10 people
initially listen to everything to figure out what they are interested in.
As time progresses, they eventually realize they only need to listen
to a portion of the floor that contains information they are interested
in. This would be identical to 10 event consumers that eventually
only want to listen to a subset of events.

Now, if you don't need the above partioning and can guarantee you'll
never need it, then move forward. If you do need partitioning then
start thinking back to that controller object you were thinking about.
At that level you can have a ClassSet that contains all instances of your
classes, and the ClassSet can be coerced into giving up ClassSubSet's
that only contain specific instances. A ClassSet might have a certain
frequency that it allows broadcasts on, say 1 every second, while the
ClassSubSet could allow more frequent broadcasting since the theoretical
number of consumers would be less.

Loads of options, and a very interesting problem. Keep us aprised for what
you are doing.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Colin Cashman" <cd*@gis.net> wrote in message
news:Jrwxc.22778$Sw.16502@attbi_s51...
In a program I'm writing, I have a class that generates a certain
event. However, there may be dozens or even hundreds of instances
of this particular class, which makes adding and removing handlers
to each instance a pain. Rather than go down that route, I was
thinking of making the event static, so I would only need to add my
handlers once, to the static event, which I can then trigger from
inside my class.

That leaves me with two questions:

1. Is there a better way to handle this than using static events?

(I thought of having another class own a "master" event, but that
simply introduces more connecting code, and is a bit messier when
it comes to encapsulation.)

2. If this IS the best way, are there any "gotchas" or problems
I should be aware of when using static events?
Thanks in advance!

Colin

Nov 16 '05 #2
I cannot see from what you have described what the "pain" is in adding and
removing handlers.
When you instantiate your class, you add the handler (using a common handler
for which accepts a "sender" parameter to provide information about which
instance fired the event).
When your class goes out of scope or you destroy it, the handler is removed.

I wouldnt think that using static events would be the way to go.

You might need to provide more info on your project to get the "right"
answer :)
HTH
JB

"Colin Cashman" <cd*@gis.net> wrote in message
news:Jrwxc.22778$Sw.16502@attbi_s51...
In a program I'm writing, I have a class that generates a certain
event. However, there may be dozens or even hundreds of instances
of this particular class, which makes adding and removing handlers
to each instance a pain. Rather than go down that route, I was
thinking of making the event static, so I would only need to add my
handlers once, to the static event, which I can then trigger from
inside my class.

That leaves me with two questions:

1. Is there a better way to handle this than using static events?

(I thought of having another class own a "master" event, but that
simply introduces more connecting code, and is a bit messier when
it comes to encapsulation.)

2. If this IS the best way, are there any "gotchas" or problems
I should be aware of when using static events?
Thanks in advance!

Colin

Nov 16 '05 #3
Justin Rogers <Ju****@games4dotnet.com> wrote:
You have quite a few options, with a static event probably being the
best option. Having static firing code is probably also a good action
to take, since it serializes your eventing code.


What makes you say that? If two events fire from two different threads,
they will execute concurrently. Being static has no magic serialisation
powers here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hello!

I have been thinking about the same approach as Colin. I have a few extra
questions, which I hope you can answer.

For instance, what happens if you have a large number of instances that are
subscribing to an event? Imagine a hierarchical structure with e.g. 25.000
nodes that were subscribed to a change event. I haven't seen any examples
(patterns) or documentation that tries to give a good answer to that
problem.

Is 25.000 listeners simply too much (hence looking at another approach to
notifying the instances is worth considering)?

If you have any pointers in this direction, I would really appreciate them.
When your class goes out of scope or you destroy it, the handler is

removed.

If you're exposing the eventlistener as a public member, how do you keep
from getting dangling references (that is, instances that have gone out of
scope but haven't been gc'et because of the strong reference to the
eventlistener)?

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #5
> > You have quite a few options, with a static event probably being the
best option. Having static firing code is probably also a good action
to take, since it serializes your eventing code.
What makes you say that? If two events fire from two different threads,
they will execute concurrently.


When a static lock is placed in the method which lauches the event, no two
event can be lauched concurrently.
Being static has no magic serialisation
powers here.


I also didn't understand what was meant with serializing event code here.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #6
> I have been thinking about the same approach as Colin. I have a few extra
questions, which I hope you can answer.

For instance, what happens if you have a large number of instances that are subscribing to an event? Imagine a hierarchical structure with e.g. 25.000
nodes that were subscribed to a change event. I haven't seen any examples
(patterns) or documentation that tries to give a good answer to that
problem.

Is 25.000 listeners simply too much (hence looking at another approach to
notifying the instances is worth considering)?

If you have any pointers in this direction, I would really appreciate them.
When your class goes out of scope or you destroy it, the handler is removed.


The real question is: Do you really need events for this? Since the nodes
should already have an reference of its parent,
why not simply call a method on each node from the parent control to
simulate an event.
If you're exposing the eventlistener as a public member, how do you keep
from getting dangling references (that is, instances that have gone out of
scope but haven't been gc'et because of the strong reference to the
eventlistener)?


Forgetting to unsubscribe events before going out of scope is indeed a
problem for .NET. In the case of a parent control with nodes you can ensure
that NodeCollection.Add adds the handler and NodeCollection.Remove which
removed an item also unsubscribes the event.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #7
cody <no****************@gmx.net> wrote:
You have quite a few options, with a static event probably being the
best option. Having static firing code is probably also a good action
to take, since it serializes your eventing code.


What makes you say that? If two events fire from two different threads,
they will execute concurrently.


When a static lock is placed in the method which lauches the event, no two
event can be lauched concurrently.


Sure - but that could be done whether or not the event is static.
Being static has no magic serialisation
powers here.


I also didn't understand what was meant with serializing event code here.


Indeed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
> I cannot see from what you have described what the "pain" is in adding and
removing handlers.
The "pain" is that it goes against the structure of the code. The code
goes something like:

Class A (a singleton) contains class B, which in turn contains a
collection of hundreds or thousands of event generators (class C).
Elsewhere in the application, there are numerous other objects (D, E,
and F) that are interested in knowing when a particular change occurs in
the instances of class C. The class B is private to class A, and the
collections class instance (that contain the instances of C) is private
to class B.

So the "pain" is having to provide various pass-throughs in classes A
and B in order for classes D, E, and F to obtain the collection of event
generators so that they can attach their handlers.
When your class goes out of scope or you destroy it, the handler is removed.


My understanding is that having a handler attached to an instance makes
it harder for the GC to reclaim that instance (or, at least, makes
reclaiming the memory take longer).
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Duwayne | last post by:
i am having a problem with event not firing. I dont know how to explain why because most of the time it fires, but when it does not fire and the page refreshes, the textboxes become unpopulated or...
0
by: | last post by:
Is this a good or bad thing.. I made this control that contains a collection of types, and its up to the caller to register a type in this controls collection. I want to render the contents...
1
by: AA | last post by:
I want to Deserialize this simple xml... ==================================================================== <SubmitReq...
1
by: rs | last post by:
Hi, I have a placeholder control, which I populate dynamically with one of the web user controls depending upon some criteria. I have a data grid and a drop in the the user control. The way...
0
by: Alexander Czernay | last post by:
I created a VB.NET-PowerPoint-AddIn as described in the VisualStudio 2003 documentation. That works very well. The AddIn adds a new toolbar to PowerPoint if the active document is based on a...
0
by: Dirk | last post by:
Hello, I have a class library with many classes and properties. For every property I have a PropertyNameChanged event. In my user interface layer it sometimes happens that I subscribe such an...
5
by: BLUE | last post by:
I have a static class, so I've declared all variables static and also the event. This is the OnEvent method: { if (EventName != null) { // Invokes the delegates EventName(this, e); }
3
by: Brennan Finighan | last post by:
Hello I am using VS 2005 with .net 2 with C#. I have a form with a panel in which I am dynamically creating combo boxes. They are all using the same data source.The reason they are dynamically...
10
by: parez | last post by:
Hi, I have implemented a static event. Is there anything special that i have do? Is thread safety an issue?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.