Raise events on multiple instances of a class

Is there a way to raise events in a class, that has multiple instances in multiple windows?

I have a class. This class resides in a window (Main Project Window).

I have several other windows with instances of this class. When I chance a property on one window I want the other windows to handle accordingly.
If I change a property in the class I can raise an event. But this event is only raised in one instance of a class, the one where I changed the property.

I tried to play around with Shared Properties. But then I need to have a timer (and “addHandler” methods) to check the state of a shared property to launch events.

Is there another way?

Have a look at the observer pattern: source.

A class doesn’t know about it’s instances, so you will need to track them yourself in a shared array or dictionary. Use WeakRefs and make sure an instance adds itself in a constructor and removes itself in the destructor. Then the class/shared code can use the property to broadcast out to all it’s instances.

Keep a list of references (WeakRef) to all instances and add your entries in Constructor and remove them in destructor.
Now you keep the list as an static array in the class and you can call stuff on them.

Why WeakRefs and not just the instance itself? Because if you add the instance directly, its Destructor will never fire.

Thanks Christian.

I tried it with a simple chat app. Of course, it was only for testing. And chatting with my self this way does not make much sense. But it helped me to understand WeakRef.

For the ones who try the same thing… I have my test app here: LINK
This sends messages to other other windows. Of course you can use this to send all kinds of data. Like JSON. This way you can have some kind of main-project window. And some floating windows that may contain properties etc. When a property changes, it sends info to any listening windows that can deal with it accordingly.

At least, that is what I am going to do.

This really got me started. I will try to create some Drag-and-drop classes, that interact with each other. But neater. When done, I will post the classes here.

For something like this I would suggest following James advice & implementing the observer pattern