private void ProcessEvents(object sender, DoWorkEventArgs e) { RemoveItems = new List <IEventListener>(); while (!_BackgroundWorker.CancellationPending) { IAction action = Actions.Take(); switch (action.GetActionType()) { case ActionType.OfferEvent: OfferEventAction offerEventAction = (OfferEventAction)action; ProcessOfferEventAction(offerEventAction); break; case ActionType.AddListener: AddListenerAction addListenerAction = (AddListenerAction)action; ProcessAddListenerAction(addListenerAction); break; case ActionType.RemoveListener: RemoveListenerAction removeListenerAction = (RemoveListenerAction)action; ProcessRemoveListenerAction(removeListenerAction); break; } } e.Cancel = true; }
private void ProcessRemoveListenerAction(RemoveListenerAction removeListenerAction) { if (removeListenerAction != null) { IEventListener eventListener = removeListenerAction.GetEventListener(); if (eventListener != null) { List <WeakReference <IEventListener> > weakReferences; foreach (EventType eventType in eventListener.GetNotifyTypes()) { if (EventTypesToListeners.TryGetValue(eventType, out weakReferences)) { foreach (WeakReference <IEventListener> weakReference in weakReferences) { IEventListener _eventListener; if (weakReference.TryGetTarget(out _eventListener)) { if (_eventListener.Equals(eventListener)) { weakReferences.Remove(weakReference); } } } } } } } }