I have large WPF-MVVM application in which I have 1 subscribe and 2 publish patterns.
I have subscribed an event as follows:
EventAggregator.GetEvent<StatusMessageEvent>().Subscribe(OnCommandLineStatusReturned, ThreadOption.UIThread);
callback method is:
private void OnCommandLineStatusReturned(StatusMessageEventArgs args)
{
//some data to display
}
now I have to publish this event two times with two different objects.
private StatusMessageEventArgs statusMessageEventArgs;
private StatusMessageEventArgs responsestatusMessageEventArgs;
I published these instances at different times,
EventAggregator.GetEvent<StatusMessageEvent>().Publish(statusMessageEventArgs);
EventAggregator.GetEvent<StatusMessageEvent>().Publish(responsestatusMessageEventArgs);
But when I run application, it misbehaves. i.e. out of 10 times, 2 times callback method doesn’t get invoked at all. is it because of 2 publish for same subscribe?
I got solution for this problem on following link:
keepSubscriberReferenceAlive
It says, If you are raising multiple events in a short period of time and have noticed performance concerns with them, you may need to subscribe with strong delegate references.
By default, CompositePresentationEvent maintains a weak delegate reference to the subscriber’s handler and filter on subscription. This means the reference that CompositePresentationEvent holds on to will not prevent garbage collection of the subscriber. Using a weak delegate reference relieves the subscriber from the need to unsubscribe and allows for proper garbage collection.
maintaining this weak delegate reference is slower than a corresponding strong reference. For most applications, this performance will not be noticeable, but if your application publishes a large number of events in a short period of time, you may need to use strong references with CompositePresentationEvent.
I modified my subscribe method as follows.