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 ProcessOfferEventAction(OfferEventAction offerEventAction) { IEvent _event = offerEventAction.GetEvent(); if (_event != null) { List <WeakReference <IEventListener> > activeListeners; if (EventTypesToListeners.TryGetValue(_event.GetEventType(), out activeListeners)) { List <WeakReference <IEventListener> > removeListeners = new List <WeakReference <IEventListener> >(); foreach (WeakReference <IEventListener> weakReference in activeListeners) { IEventListener eventListener; if (weakReference.TryGetTarget(out eventListener)) { eventListener.OnEvent(_event); } else { removeListeners.Add(weakReference); } } foreach (WeakReference <IEventListener> remove in removeListeners) { activeListeners.Remove(remove); } } } }