static public void AddListener(string eventType, cCallback handler) { // Obtain a lock on the event table to keep this thread-safe. lock (eventTable) { // Create an entry for this event type if it doesn't already exist. if (!eventTable.ContainsKey(eventType)) { eventTable.Add(eventType, null); } // Add the handler to the event. eventTable[eventType] = (cCallback)eventTable[eventType] + handler; } }
public static void AddListener(string eventType, cCallback handler) { // Obtain a lock on the event table to keep this thread-safe. lock (eventTable) { // Create an entry for this event type if it doesn't already exist. if (!eventTable.ContainsKey(eventType)) { eventTable.Add(eventType, null); } // Add the handler to the event. eventTable[eventType] = (cCallback)eventTable[eventType] + handler; } }
static public void Invoke(string eventType) { Delegate d; // Invoke the delegate only if the event type is in the dictionary. if (eventTable.TryGetValue(eventType, out d)) { // Take a local copy to prevent a race condition if another thread // were to unsubscribe from this event. cCallback callback = (cCallback)d; // Invoke the delegate if it's not null. if (callback != null) { callback(); } } }
static public void RemoveListener(string eventType, cCallback handler) { // Obtain a lock on the event table to keep this thread-safe. lock (eventTable) { // Only take action if this event type exists. if (eventTable.ContainsKey(eventType)) { // Remove the event handler from this event. eventTable[eventType] = (cCallback)eventTable[eventType] - handler; // If there's nothing left then remove the event type from the event table. if (eventTable[eventType] == null) { eventTable.Remove(eventType); } } } }
public static void RemoveListener(string eventType, cCallback handler) { // Obtain a lock on the event table to keep this thread-safe. lock (eventTable) { // Only take action if this event type exists. if (eventTable.ContainsKey(eventType)) { // Remove the event handler from this event. eventTable[eventType] = (cCallback)eventTable[eventType] - handler; // If there's nothing left then remove the event type from the event table. if (eventTable[eventType] == null) { eventTable.Remove(eventType); } } } }