private WeakMulticastDelegate Remove(Delegate realDelegate)
        {
            WeakMulticastDelegate result;

            if (this.Equals(realDelegate))
            {
                result = this.prev;
            }
            else
            {
                WeakMulticastDelegate current = this.prev;
                WeakMulticastDelegate last    = this;
                while (current != null)
                {
                    if (current.Equals(realDelegate))
                    {
                        last.prev    = current.prev;
                        current.prev = null;
                        break;
                    }
                    last    = current;
                    current = current.prev;
                }
                result = this;
            }
            return(result);
        }
        /// <summary>
        /// Invokes the delegate with the given arguments
        /// If one target throws an exception the other targets
        /// won't be handled anymore.
        /// </summary>
        /// <param name="args">the argument array to pass to the target</param>
        public void Invoke(object[] args)
        {
            WeakMulticastDelegate current = this;

            while (current != null)
            {
                int start = Environment.TickCount;

                if (current.weakRef == null)
                {
                    current.method.Invoke(null, args);
                }
                else if (current.weakRef.IsAlive)
                {
                    current.method.Invoke(current.weakRef.Target, args);
                }

                if (Environment.TickCount - start > 500)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("Invoke took " + (Environment.TickCount - start) + "ms! " + current.ToString());
                    }
                }
                current = current.prev;
            }
        }
 /// <summary>
 /// Adds an event handler to the list
 /// </summary>
 /// <param name="e">The event from which we add a handler</param>
 /// <param name="del">The callback method</param>
 public void AddHandler(RoadEvent e, RoadEventHandler del)
 {
     try
     {
         m_lock.AcquireWriterLock(TIMEOUT);
         try
         {
             WeakMulticastDelegate deleg = (WeakMulticastDelegate)m_events[e];
             if (deleg == null)
             {
                 m_events[e] = new WeakMulticastDelegate(del);
             }
             else
             {
                 m_events[e] = WeakMulticastDelegate.Combine(deleg, del);
             }
         }
         finally
         {
             m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException ex)
     {
         if (log.IsErrorEnabled)
             log.Error("Failed to add event handler!", ex);
     }
 }
Beispiel #4
0
 public void Invoke(object[] args)
 {
     for (WeakMulticastDelegate weakMulticastDelegate = this; weakMulticastDelegate != null; weakMulticastDelegate = weakMulticastDelegate.prev)
     {
         int tickCount = Environment.TickCount;
         if (weakMulticastDelegate.weakRef == null)
         {
             weakMulticastDelegate.method.Invoke(null, args);
         }
         else
         {
             if (weakMulticastDelegate.weakRef.IsAlive)
             {
                 weakMulticastDelegate.method.Invoke(weakMulticastDelegate.weakRef.Target, args);
             }
         }
         if (Environment.TickCount - tickCount > 500 && WeakMulticastDelegate.log.IsWarnEnabled)
         {
             WeakMulticastDelegate.log.Warn(string.Concat(new object[]
             {
                 "Invoke took ",
                 Environment.TickCount - tickCount,
                 "ms! ",
                 weakMulticastDelegate.ToString()
             }));
         }
     }
 }
 public void InvokeSafe(object[] args)
 {
     for (WeakMulticastDelegate current = this; current != null; current = current.prev)
     {
         int start = Environment.TickCount;
         try
         {
             if (current.weakRef == null)
             {
                 current.method.Invoke(null, args);
             }
             else
             {
                 if (current.weakRef.IsAlive)
                 {
                     current.method.Invoke(current.weakRef.Target, args);
                 }
             }
         }
         catch (Exception ex)
         {
             LogProvider.Default.Error("InvokeSafe", ex);
         }
         if (Environment.TickCount - start > 500)
         {
             LogProvider.Default.Warn(string.Concat(new object[]
             {
                 "InvokeSafe took ",
                 Environment.TickCount - start,
                 "ms! ",
                 current.ToString()
             }));
         }
     }
 }
 /// <summary>
 /// Combines this weak multicast delegate with a normal delegate
 /// </summary>
 /// <param name="realDelegate">the normal delegate</param>
 /// <returns>this delegate</returns>
 private WeakMulticastDelegate Combine(Delegate realDelegate)
 {
     WeakMulticastDelegate head = new WeakMulticastDelegate(realDelegate);
     head.prev = this.prev;
     this.prev = head;
     return this;
 }
 /// <summary>
 /// Combines a weak multicast delegate with a normal delegate
 /// and makes sure the normal delegate has not been added yet.
 /// </summary>
 /// <param name="weakDelegate">the weak multicast delegate</param>
 /// <param name="realDelegate">the normal delegate</param>
 /// <returns>the new combined weak multicast delegate</returns>
 public static WeakMulticastDelegate CombineUnique(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
 {
     if (realDelegate == null)
     {
         return(null);
     }
     return((weakDelegate == null) ? new WeakMulticastDelegate(realDelegate) : weakDelegate.CombineUnique(realDelegate));
 }
Beispiel #8
0
 public static WeakMulticastDelegate Remove(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
 {
     if (realDelegate == null || weakDelegate == null)
     {
         return(null);
     }
     return(weakDelegate.Remove(realDelegate));
 }
Beispiel #9
0
 private WeakMulticastDelegate Combine(Delegate realDelegate)
 {
     this.prev = new WeakMulticastDelegate(realDelegate)
     {
         prev = this.prev
     };
     return(this);
 }
        /// <summary>
        /// Combines this weak multicast delegate with a normal delegate
        /// </summary>
        /// <param name="realDelegate">the normal delegate</param>
        /// <returns>this delegate</returns>
        private WeakMulticastDelegate Combine(Delegate realDelegate)
        {
            WeakMulticastDelegate head = new WeakMulticastDelegate(realDelegate);

            head.prev = this.prev;
            this.prev = head;
            return(this);
        }
Beispiel #11
0
 public static WeakMulticastDelegate Combine(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
 {
     if (realDelegate == null)
     {
         return(null);
     }
     if (weakDelegate != null)
     {
         return(weakDelegate.Combine(realDelegate));
     }
     return(new WeakMulticastDelegate(realDelegate));
 }
        public static WeakMulticastDelegate Remove(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
        {
            WeakMulticastDelegate result;

            if (realDelegate == null || weakDelegate == null)
            {
                result = null;
            }
            else
            {
                result = weakDelegate.Remove(realDelegate);
            }
            return(result);
        }
        public static WeakMulticastDelegate CombineUnique(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
        {
            WeakMulticastDelegate result;

            if (realDelegate == null)
            {
                result = null;
            }
            else
            {
                result = ((weakDelegate == null) ? new WeakMulticastDelegate(realDelegate) : weakDelegate.CombineUnique(realDelegate));
            }
            return(result);
        }
        /// <summary>
        /// Combines this weak multicast delegate with a normal delegate
        /// Makes sure the delegate target has not been added yet
        /// </summary>
        /// <param name="realDelegate">the real delegate</param>
        /// <returns>the new weak multicast delegate</returns>
        private WeakMulticastDelegate CombineUnique(Delegate realDelegate)
        {
            bool found = Equals(realDelegate);

            if (!found && prev != null)
            {
                WeakMulticastDelegate curNode = prev;
                while (!found && curNode != null)
                {
                    if (curNode.Equals(realDelegate))
                    {
                        found = true;
                    }
                    curNode = curNode.prev;
                }
            }
            return(found ? this : Combine(realDelegate));
        }
Beispiel #15
0
        public string Dump()
        {
            StringBuilder         stringBuilder         = new StringBuilder();
            WeakMulticastDelegate weakMulticastDelegate = this;
            int num = 0;

            while (weakMulticastDelegate != null)
            {
                num++;
                if (weakMulticastDelegate.weakRef == null)
                {
                    stringBuilder.Append("\t");
                    stringBuilder.Append(num);
                    stringBuilder.Append(") ");
                    stringBuilder.Append(weakMulticastDelegate.method.Name);
                    stringBuilder.Append(Environment.NewLine);
                }
                else
                {
                    if (weakMulticastDelegate.weakRef.IsAlive)
                    {
                        stringBuilder.Append("\t");
                        stringBuilder.Append(num);
                        stringBuilder.Append(") ");
                        stringBuilder.Append(weakMulticastDelegate.weakRef.Target);
                        stringBuilder.Append(".");
                        stringBuilder.Append(weakMulticastDelegate.method.Name);
                        stringBuilder.Append(Environment.NewLine);
                    }
                    else
                    {
                        stringBuilder.Append("\t");
                        stringBuilder.Append(num);
                        stringBuilder.Append(") INVALID.");
                        stringBuilder.Append(weakMulticastDelegate.method.Name);
                        stringBuilder.Append(Environment.NewLine);
                    }
                }
                weakMulticastDelegate = weakMulticastDelegate.prev;
            }
            return(stringBuilder.ToString());
        }
        /// <summary>
        /// Dumps the delegates in this multicast delegate to a string
        /// </summary>
        /// <returns>The string containing the formated dump</returns>
        public string Dump()
        {
            StringBuilder         builder = new StringBuilder();
            WeakMulticastDelegate current = this;
            int count = 0;

            while (current != null)
            {
                count++;
                if (current.weakRef == null)
                {
                    builder.Append("\t");
                    builder.Append(count);
                    builder.Append(") ");
                    builder.Append(current.method.Name);
                    builder.Append(System.Environment.NewLine);
                }
                else
                {
                    if (current.weakRef.IsAlive)
                    {
                        builder.Append("\t");
                        builder.Append(count);
                        builder.Append(") ");
                        builder.Append(current.weakRef.Target);
                        builder.Append(".");
                        builder.Append(current.method.Name);
                        builder.Append(Environment.NewLine);
                    }
                    else
                    {
                        builder.Append("\t");
                        builder.Append(count);
                        builder.Append(") INVALID.");
                        builder.Append(current.method.Name);
                        builder.Append(Environment.NewLine);
                    }
                }
                current = current.prev;
            }
            return(builder.ToString());
        }
Beispiel #17
0
        private WeakMulticastDelegate CombineUnique(Delegate realDelegate)
        {
            bool flag = this.Equals(realDelegate);

            if (!flag && this.prev != null)
            {
                WeakMulticastDelegate weakMulticastDelegate = this.prev;
                while (!flag && weakMulticastDelegate != null)
                {
                    if (weakMulticastDelegate.Equals(realDelegate))
                    {
                        flag = true;
                    }
                    weakMulticastDelegate = weakMulticastDelegate.prev;
                }
            }
            if (!flag)
            {
                return(this.Combine(realDelegate));
            }
            return(this);
        }
Beispiel #18
0
        private WeakMulticastDelegate Remove(Delegate realDelegate)
        {
            if (this.Equals(realDelegate))
            {
                return(this.prev);
            }
            WeakMulticastDelegate weakMulticastDelegate  = this.prev;
            WeakMulticastDelegate weakMulticastDelegate2 = this;

            while (weakMulticastDelegate != null)
            {
                if (weakMulticastDelegate.Equals(realDelegate))
                {
                    weakMulticastDelegate2.prev = weakMulticastDelegate.prev;
                    weakMulticastDelegate.prev  = null;
                    break;
                }
                weakMulticastDelegate2 = weakMulticastDelegate;
                weakMulticastDelegate  = weakMulticastDelegate.prev;
            }
            return(this);
        }
Beispiel #19
0
 public static WeakMulticastDelegate operator -(WeakMulticastDelegate d, Delegate realD)
 {
     return(WeakMulticastDelegate.Remove(d, realD));
 }
Beispiel #20
0
 public static WeakMulticastDelegate operator +(WeakMulticastDelegate d, Delegate realD)
 {
     return(WeakMulticastDelegate.Combine(d, realD));
 }
 /// <summary>
 /// Removes a normal delegate from a weak multicast delegate
 /// </summary>
 /// <param name="weakDelegate">the weak multicast delegate</param>
 /// <param name="realDelegate">the normal delegate</param>
 /// <returns>the new weak multicast delegate</returns>
 public static WeakMulticastDelegate Remove(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
 {
     if (realDelegate == null || weakDelegate == null)
         return null;
     return weakDelegate.Remove(realDelegate);
 }
 /// <summary>
 /// Combines a weak multicast delegate with a normal delegate
 /// and makes sure the normal delegate has not been added yet.
 /// </summary>
 /// <param name="weakDelegate">the weak multicast delegate</param>
 /// <param name="realDelegate">the normal delegate</param>
 /// <returns>the new combined weak multicast delegate</returns>
 public static WeakMulticastDelegate CombineUnique(WeakMulticastDelegate weakDelegate, Delegate realDelegate)
 {
     if (realDelegate == null)
         return null;
     return (weakDelegate == null) ? new WeakMulticastDelegate(realDelegate) : weakDelegate.CombineUnique(realDelegate);
 }