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)
                    {
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public void PublishEvent <E>(E message) where E : class
 {
     lock (monitor)
     {
         if (shutdown)
         {
             throw new InvalidOperationException();
         }
         foreach (var v in events)
         {
             if (v.Key.Equals(typeof(E)))
             {
                 foreach (Subscribers s in v.Value)
                 {
                     s.message = message;
                     SyncUtils.Notify(monitor, s.con);
                 }
             }
         }
     }
 }