public static void RemoveAllHandlersForObject(object obj) { if (obj == null) { throw new ArgumentNullException("obj", "No object given!"); } DOLEventHandlerCollection col = null; if (Lock.TryEnterReadLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.TryGetValue(obj, out col); } finally { Lock.ExitReadLock(); } } else { log.ErrorFormat("Timeout exceeded (Read) on attempt to RemoveAllHandlersForObject: {0}", obj.ToString()); } if (col != null) { col.RemoveAllHandlers(); if (Lock.TryEnterWriteLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.Remove(obj); } finally { Lock.ExitWriteLock(); } } else { log.ErrorFormat("Timeout exceeded (Write) on attempt to RemoveAllHandlersForObject: {0}", obj.ToString()); } } }
/// <summary> /// Removes a single event handler from an object. /// You need to have registered the event before being /// able to remove it. /// </summary> /// <param name="obj">The object that needs to be the sender of events</param> /// <param name="e">The event type from which to deregister</param> /// <param name="del">The event handler to deregister for this event type</param> /// <exception cref="ArgumentNullException">If one of the parameters is null</exception> public static void RemoveHandler(object obj, DOLEvent e, DOLEventHandler del) { if (obj == null) { throw new ArgumentNullException("obj", "No object given!"); } if (e == null) { throw new ArgumentNullException("e", "No event type given!"); } if (del == null) { throw new ArgumentNullException("del", "No event handler given!"); } DOLEventHandlerCollection col = null; if (Lock.TryEnterReadLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.TryGetValue(obj, out col); } finally { Lock.ExitReadLock(); } } else { log.ErrorFormat("Timeout exceeded on attempt to RemoveHandler for object: {0}, event: {1}", obj.ToString(), e.Name); } if (col != null) { col.RemoveHandler(e, del); } }
static GameEventMgr() { Lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); m_gameObjectEventCollections = new Dictionary<object, DOLEventHandlerCollection>(); m_globalHandlerCollection = new DOLEventHandlerCollection(); }
/// <summary> /// Registers a single local event handler. /// Local event handlers will only be called if the /// "sender" parameter in the Notify method equals /// the object for which a local event handler was /// registered. /// </summary> /// <remarks> /// Certain events will never have a local event handler. /// This happens if the Notify method is called without /// a sender parameter for example! /// </remarks> /// <param name="obj">The object that needs to be the sender of events</param> /// <param name="e">The event type to register for</param> /// <param name="del">The event handler to register for this event type</param> /// <param name="unique">Flag wether event shall be added unique or not</param> /// <exception cref="ArgumentNullException">If one of the parameters is null</exception> private static void AddHandler(object obj, DOLEvent e, DOLEventHandler del, bool unique) { if(obj == null) throw new ArgumentNullException("obj", "No object given!"); if(e == null) throw new ArgumentNullException("e", "No event type given!"); if(del == null) throw new ArgumentNullException("del", "No event handler given!"); if(!e.IsValidFor(obj)) throw new ArgumentException("Object is not valid for this event type", "obj"); if(Lock.TryEnterUpgradeableReadLock(LOCK_TIMEOUT)) { try { DOLEventHandlerCollection col; if(!m_gameObjectEventCollections.TryGetValue(obj, out col)) { col = new DOLEventHandlerCollection(); if (Lock.TryEnterWriteLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.Add(obj, col); } finally { Lock.ExitWriteLock(); } } else { log.ErrorFormat("Timeout exceeded on attempt to AddHandler for object: {0}, event: {1}", obj.ToString(), e.Name); } } if(unique) { col.AddHandlerUnique(e, del); } else { col.AddHandler(e, del); } } finally { Lock.ExitUpgradeableReadLock(); } } }
static GameEventMgr() { Lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); m_gameObjectEventCollections = new Dictionary <object, DOLEventHandlerCollection>(); m_globalHandlerCollection = new DOLEventHandlerCollection(); }
/// <summary> /// Registers a single local event handler. /// Local event handlers will only be called if the /// "sender" parameter in the Notify method equals /// the object for which a local event handler was /// registered. /// </summary> /// <remarks> /// Certain events will never have a local event handler. /// This happens if the Notify method is called without /// a sender parameter for example! /// </remarks> /// <param name="obj">The object that needs to be the sender of events</param> /// <param name="e">The event type to register for</param> /// <param name="del">The event handler to register for this event type</param> /// <param name="unique">Flag wether event shall be added unique or not</param> /// <exception cref="ArgumentNullException">If one of the parameters is null</exception> private static void AddHandler(object obj, DOLEvent e, DOLEventHandler del, bool unique) { if (obj == null) { throw new ArgumentNullException("obj", "No object given!"); } if (e == null) { throw new ArgumentNullException("e", "No event type given!"); } if (del == null) { throw new ArgumentNullException("del", "No event handler given!"); } if (!e.IsValidFor(obj)) { throw new ArgumentException("Object is not valid for this event type", "obj"); } if (Lock.TryEnterUpgradeableReadLock(LOCK_TIMEOUT)) { try { DOLEventHandlerCollection col; if (!m_gameObjectEventCollections.TryGetValue(obj, out col)) { col = new DOLEventHandlerCollection(); if (Lock.TryEnterWriteLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.Add(obj, col); } finally { Lock.ExitWriteLock(); } } else { log.ErrorFormat("Timeout exceeded on attempt to AddHandler for object: {0}, event: {1}", obj.ToString(), e.Name); } } if (unique) { col.AddHandlerUnique(e, del); } else { col.AddHandler(e, del); } } finally { Lock.ExitUpgradeableReadLock(); } } }