Ejemplo n.º 1
0
        public void SubscribeEvent <T>(Action <T> handler) where T : class
        {
            Subscribers subs = new Subscribers(maxPending);

            lock (monitor)
            {
                while (true)
                {
                    if (shutdown)
                    {
                        return;
                    }
                    if (!events.ContainsKey(typeof(T)))
                    {
                        events.Add(typeof(T), new LinkedList <Subscribers>());
                        events[typeof(T)].AddLast(subs);
                    }
                    else
                    {
                        if (events[typeof(T)].Count < subs.getMaxPending())
                        {
                            events[typeof(T)].AddLast(subs);
                        }
                    }
                    try
                    {
                        SyncUtils.Wait(monitor, subs.con);
                        subs.execute(handler);
                        events[typeof(T)].Remove(subs);
                        if (shutdown)
                        {
                            while (events[typeof(T)].Contains(subs))
                            {
                                ;
                            }
                            events[typeof(T)].Remove(subs);
                            return;
                        }
                    }
                    catch (ThreadInterruptedException e)
                    {
                    }
                }
            }
        }