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 8 12524
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.2277 8$Sw.16502@attb i_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
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.2277 8$Sw.16502@attb i_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
Justin Rogers <Ju****@games4d otnet.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
--
> > 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
> 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
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
> 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). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
unbinded etc.
Here are the code that will make the error.
http://home.comcast.net/~dmsy/QuoteMain.aspx
http://home.comcast.net/~dmsy/QuoteMain.aspx.cs
http://home.comcast.net/~dmsy/SaleInfo.aspx
|
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 as it arrives via a method call on these
registered types so I have a static event fired (as its beind subscribed by
the container and not per isntance, ill send the instance in the event args)
Is there a better way to do this than a static event?
|
by: AA |
last post by:
I want to Deserialize this simple xml...
====================================================================
<SubmitReq xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1 -3">
<MM7Version>5.6.0</MM7Version>
<SenderIdentification>
<VASPID>TNN</VASPID>
<VASID>News</VASID>
</SenderIdentification>
<Recipients>
|
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 it should work is, when a value in the drop down control(in
the web user control loaded in the place holder control) is selected
the data grid should load information accordingly.
|
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 certain master. So I had to catch all events that notify a
change of the active presentation.
For that, I made two approaches:
1. A method that handles the apropriate events directly via HANDLES:
| |
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 event from multiple
places.
For example, I have a Property "Active" on a class. This property is
bounded to a CheckBox control in a user control. When this property
changes to false, there is one code which removes the UserControl from
|
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);
}
|
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 created is because the program does not how many are required. When I change the selected index for any one of the comboboxes they all change to the same selected index. Any ideas ??. I have tried creating the event as a delegete inside the method...
|
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?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |