问题
In my Composite WPF application I have an event that is published when the user double-clicks on a control. Modules subscribe to the event and perform an action when necessary.
This event seems to stop working at random. Sometimes when I run the application I can trigger the event with no problems, other times I can only trigger it a few times before the modules stop receiving the event.
When I look in the debugger the CAL EventAggregator
still has the event, but the event has no subscriptions. How can EventAggregator
be losing subscriptions?
回答1:
Turns out it was the garbage collector removing the subscriptions. I'll have to read up on the internals, but when I replaced
this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(SomeFunction);
with
this.mEventAggregator.GetEvent<SomeEvent>().Subscribe(
SomeFunction, ThreadOption.UIThread, true);
it started working. The UI thread parameters wasn't my problem, but for others it may be important to ensure you're handling the event on the right thread too.
来源:https://stackoverflow.com/questions/1132690/composite-wpf-eventaggregator-subscriptions-being-lost