Example #1
0
        /// <summary>
        /// Saves a subscription to a certain event.
        /// </summary>
        /// <param name="action">The action that will be executed synchronously when an event of the specified type is published.</param>
        public EventSubscriptionHandle Subscribe <T>(Action <T> action) where T : BaseEvent
        {
            EventSubscriptionHandle handle;

            Monitor.Enter(eventSubscriptionLists);

            EventSubscription <T>     eventSubscription = new EventSubscription <T>(action);
            EventSubscriptionList <T> eventSubscriptionList;

            handle = new EventSubscriptionHandle(eventSubscription);

            if (eventSubscriptionLists.ContainsKey(typeof(T)))
            {
                eventSubscriptionList = (EventSubscriptionList <T>)eventSubscriptionLists[typeof(T)];
            }
            else
            {
                eventSubscriptionList = new EventSubscriptionList <T>();

                eventSubscriptionLists[typeof(T)] = eventSubscriptionList;
            }

            eventSubscriptionList.Add(eventSubscription);

            Monitor.Exit(eventSubscriptionLists);

            return(handle);
        }
Example #2
0
        /// <summary>
        /// Publishes an event. Never call this outside of the main scheduler thread.
        /// </summary>
        /// <param name="e">The event that will be published.</param>
        public void Publish <T>(T e) where T : BaseEvent
        {
            Monitor.Enter(eventSubscriptionLists);

            if (eventSubscriptionLists.ContainsKey(typeof(T)))
            {
                EventSubscriptionList <T> eventSubscriptionList = (EventSubscriptionList <T>)eventSubscriptionLists[typeof(T)];

                eventSubscriptionList.ForEach(

                    (EventSubscription <T> eventSubscription) =>
                {
                    eventSubscription.Action.Invoke(e);
                }

                    );
            }

            Monitor.Exit(eventSubscriptionLists);
        }