background preloader

Event

Facebook Twitter

Edjez's WebLog : CAB -Event Broker: Pub-Sub for your compon. Yesterday we finished some cleanup refactorings around the EventBroker feature of CAB.

edjez's WebLog : CAB -Event Broker: Pub-Sub for your compon

I'm happy with the result, because it's conceptually simple, it's easy to use, and it isn't much code, yet it helps a lot. I expect some things to be added before release but here is pretty much the core of it as it is right now. The Challenge A lot of the CAB features revolve around integrating components in complex, "smart client" apps. A good pattern to integrate components and notifications is to use Pub-Sub (publisher-subscriber) mechanisms between the services.

Notifications & events one-way are necessary in UI design between visual and non-visual components Publishers & subscribers don’t know about each other You may have sources without handlers and viceversa CAB Solution CAB has the following concepts around pub-sub: Publishers are components that want to raise events, subscribers are components that have methods that are called when events get fired. Events have an Event Topic.

If (ApplesFalling! EventBroker: a notification component for synchronous and asynch. Download source - 224 KB Please see the Download section below for instructions on how to run the code.

EventBroker: a notification component for synchronous and asynch

Preface This article is a response to the article Weak Events in C# by Daniel Grunwald. Introduction EventBroker is a component that you can use to fire and receive notifications in your system. Features Loose Coupling: The subscriber does not have to know the publisher. Thread Synchronization: The subscriber defines on which thread the subscription handler is executed: Same thread as publisher (synchronous) Background thread (asynchronous) User interface thread (synchronous or asynchronous) Publishers can restrict subscriptions to events to be synchronous or asynchronous.

Multiple Publishers/Subscribers: Several publishers/subscribers can fire/handle the same EventTopic. Weak References: Publishers and subscribers are referenced by weak references which will not prevent them from being garbage collected – like in the normal event registration model. Scope: Scope per EventBroker: Background Simple.

Weak Events in C#. Free source code and programming help. Download source code - 16.42 KB Table of contents Introduction When using normal C# events, registering an event handler creates a strong reference from the event source to the listening object. If the source object has a longer lifetime than the listener, and the listener doesn't need the events anymore when there are no other references to it, using normal .NET events causes a memory leak: the source object holds listener objects in memory that should be garbage collected. There are lots of different approaches to this problem. What Exactly are Events?

Many programmers think events are a list of delegates - that's simply wrong. EventHandler eh = Method1; eh += Method2; So, what then are events? Properties essentially are a pair of get/set-methods. Public event EventHandler MyEvent { add { ... } remove { ... } } Only adding and removing handlers is public. Now, what leads to confusion sometimes is that C# has a short-hand syntax: public event EventHandler MyEvent; This expands to: if (MyEvent !