1
votes

The question is simple, really. Does the EventAggregator buffer events? Example: A single Subscriber listening for 2 different events, a GO and a NO-GO. Each event requires some work, database updates, notifying the UI etc. There could be as many as 64 publishers. The publishers work in near real-time so GO or NO-GOs could come very quickly. So can the Event Aggregator buffer these so the subscriber can process them in serial fashion.

Thanks, Doug

2

2 Answers

2
votes

By default the subscribe action is executed on the same thread as the event was published on. This means that the publisher won't be able to publish another event until the previous one has been handled by the subscriber. So the events are actually processed in serial fashion by default.

You can change this by specifying a ThreadOption when you subscribe:

eventAggregator.GetEvent<YourEventType>().Subscribe(s =>
            {
    //do something that make take a while but don't block the publisher...
    System.Threading.Thread.Sleep(5000);
}, ThreadOption.BackgroundThread);
1
votes

You can handle events in original order using EventLoopScheduler from Reactive Extensions. EventLoopScheduler will handle event publications in single background thread. It will not stop publisher's threads after calling of "Schedule" method

EventLoopScheduler eventLoopScheduler = new EventLoopScheduler();

eventAggregator.GetEvent<YourEventType>().Subscribe(s =>
{
    eventLoopScheduler.Schedule(() =>
    {
        //handler
    });
});

As for me, I use MessageBus from ReactiveUI instead of EventAggregator, because it can take IScheduler as argument and can remember last event payload after unsubscription.