示例#1
0
        public bool Remove(string eventName, MessageHandlerAction <T> handler = null)
        {
            if (eventName.IsEmpty())
            {
                return(false);
            }

            try
            {
                _lock.EnterWriteLock();
                var key = eventName.ToLower();
                if (_specificHandlers.ContainsKey(key))
                {
                    if (handler == null)
                    {
                        _specificHandlers.Remove(key);
                        return(true);
                    }
                    else
                    {
                        return(_specificHandlers[key].Remove(handler));
                    }
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
示例#2
0
        public void Add(string eventName, MessageHandlerAction <T> handler)
        {
            try
            {
                _lock.EnterWriteLock();
                List <MessageHandlerAction <T> > result;
                var key = eventName.ToLower();
                if (_specificHandlers.TryGetValue(key, out result))
                {
                    if (result != null)
                    {
                        result.Add(handler);
                        return;
                    }
                }

                _specificHandlers[key] = new List <MessageHandlerAction <T> > {
                    handler
                };
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
示例#3
0
 public static void SafeHandle <T>(this MessageHandlerAction <T> handler, T message, ILogger logger) where T : IMessage
 {
     try
     {
         handler.Handle(message);
     }
     catch (Exception ex)
     {
         logger.Error("Error notifying subscriber", ex);
     }
 }
示例#4
0
 /// <summary>Remove handler from the collection.</summary>
 /// <param name="handler"></param>
 /// <returns>True if removed, false if not found.</returns>
 public bool Remove(MessageHandlerAction <T> handler)
 {
     try
     {
         _lock.EnterWriteLock();
         return(_handlers.RemoveAll(x => x.Equals(handler)) > 0);
     }
     finally
     {
         _lock.ExitWriteLock();
     }
 }
示例#5
0
 /// <summary>Add handler to the collection.</summary>
 /// <param name="handler"></param>
 public void Add(MessageHandlerAction <T> handler)
 {
     try
     {
         _lock.EnterWriteLock();
         _handlers.Add(handler);
     }
     finally
     {
         _lock.ExitWriteLock();
     }
 }