private static void AddHandler(object obj, RoadEvent e, RoadEventHandler 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"); } try { GameEventMgr.m_lock.AcquireReaderLock(3000); try { RoadEventHandlerCollection roadEventHandlerCollection = (RoadEventHandlerCollection)GameEventMgr.m_GameObjectEventCollections[obj]; if (roadEventHandlerCollection == null) { roadEventHandlerCollection = new RoadEventHandlerCollection(); LockCookie lockCookie = GameEventMgr.m_lock.UpgradeToWriterLock(3000); try { GameEventMgr.m_GameObjectEventCollections[obj] = roadEventHandlerCollection; } finally { GameEventMgr.m_lock.DowngradeFromWriterLock(ref lockCookie); } } if (unique) { roadEventHandlerCollection.AddHandlerUnique(e, del); } else { roadEventHandlerCollection.AddHandler(e, del); } } finally { GameEventMgr.m_lock.ReleaseReaderLock(); } } catch (ApplicationException exception) { if (GameEventMgr.log.IsErrorEnabled) { GameEventMgr.log.Error("Failed to add local event handler!", exception); } } }
/// <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, RoadEvent e, RoadEventHandler del, bool unique) { //Test the parameters 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"); try { m_lock.AcquireReaderLock(TIMEOUT); try { RoadEventHandlerCollection col = (RoadEventHandlerCollection)m_GameObjectEventCollections[obj]; if (col == null) { col = new RoadEventHandlerCollection(); LockCookie lc = m_lock.UpgradeToWriterLock(TIMEOUT); try { m_GameObjectEventCollections[obj] = col; } finally { m_lock.DowngradeFromWriterLock(ref lc); } } if (unique) col.AddHandlerUnique(e, del); else col.AddHandler(e, del); } finally { m_lock.ReleaseReaderLock(); } } catch (ApplicationException ex) { if (log.IsErrorEnabled) log.Error("Failed to add local event handler!", ex); } }
/// <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, RoadEvent e, RoadEventHandler del, bool unique) { //Test the parameters 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"); } try { m_lock.AcquireReaderLock(TIMEOUT); try { RoadEventHandlerCollection col = (RoadEventHandlerCollection)m_GameObjectEventCollections[obj]; if (col == null) { col = new RoadEventHandlerCollection(); LockCookie lc = m_lock.UpgradeToWriterLock(TIMEOUT); try { m_GameObjectEventCollections[obj] = col; } finally { m_lock.DowngradeFromWriterLock(ref lc); } } if (unique) { col.AddHandlerUnique(e, del); } else { col.AddHandler(e, del); } } finally { m_lock.ReleaseReaderLock(); } } catch (ApplicationException ex) { if (log.IsErrorEnabled) { log.Error("Failed to add local event handler!", ex); } } }