public void RemoveRegisteredDelegates()
        {
            MockDelegateTarget mockDelegateTarget1 = new MockDelegateTarget();
            MockDelegateTarget mockDelegateTarget2 = new MockDelegateTarget();

            var weakMulticastDelegate = new WeakMulticastDelegate <Action <object> >();

            weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod);
            weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod);
            weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod2);
            weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod2);
            weakMulticastDelegate.Add(MockStaticDelegateTarget.DelegateMethod);

            weakMulticastDelegate.Remove(mockDelegateTarget1.DelegateMethod);
            weakMulticastDelegate.Remove(mockDelegateTarget1.DelegateMethod); // Remove twice
            weakMulticastDelegate.Remove(mockDelegateTarget2.DelegateMethod);
            weakMulticastDelegate.Remove(mockDelegateTarget2.DelegateMethod2);
            weakMulticastDelegate.Remove(MockStaticDelegateTarget.DelegateMethod);

            MockStaticDelegateTarget.Clear();
            object parameter = new object();

            weakMulticastDelegate.Invoke(parameter);

            Assert.IsFalse(mockDelegateTarget1.DelegateCalled);
            Assert.IsFalse(mockDelegateTarget2.DelegateCalled);
            Assert.IsTrue(mockDelegateTarget1.DelegateCalled2);
            Assert.IsFalse(mockDelegateTarget2.DelegateCalled2);
            Assert.IsFalse(MockStaticDelegateTarget.DelegateCalled);
            Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter2);
        }
        public void GCRemovesDelegates()
        {
            MockDelegateTarget mockDelegateTarget1 = new MockDelegateTarget();
            MockDelegateTarget mockDelegateTarget2 = new MockDelegateTarget();

            var weakMulticastDelegate = new WeakMulticastDelegate <Action <object> >();

            weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod);
            weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod);
            weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod2);
            weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod2);

            Assert.AreEqual(4, weakMulticastDelegate.Count);

            mockDelegateTarget2 = null;
            GC.Collect();
            object parameter = new object();

            weakMulticastDelegate.Invoke(parameter);
            Assert.AreEqual(2, weakMulticastDelegate.Count);
            Assert.IsTrue(mockDelegateTarget1.DelegateCalled);
            Assert.IsTrue(mockDelegateTarget1.DelegateCalled2);
            Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter);
            Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter2);

            mockDelegateTarget1 = null;
            GC.Collect();
            weakMulticastDelegate.Remove(null);
            Assert.AreEqual(0, weakMulticastDelegate.Count);

            weakMulticastDelegate.Invoke(parameter); // Dummy invoke on empty WeakDelegateManager.
        }
        public void GCRemovesDelegates()
        {
            MockDelegateTarget mockDelegateTarget1 = new MockDelegateTarget();
              MockDelegateTarget mockDelegateTarget2 = new MockDelegateTarget();

              var weakMulticastDelegate = new WeakMulticastDelegate<Action<object>>();
              weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod);
              weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod);
              weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod2);
              weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod2);

              Assert.AreEqual(4, weakMulticastDelegate.Count);

              mockDelegateTarget2 = null;
              GC.Collect();
              object parameter = new object();
              weakMulticastDelegate.Invoke(parameter);
              Assert.AreEqual(2, weakMulticastDelegate.Count);
              Assert.IsTrue(mockDelegateTarget1.DelegateCalled);
              Assert.IsTrue(mockDelegateTarget1.DelegateCalled2);
              Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter);
              Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter2);

              mockDelegateTarget1 = null;
              GC.Collect();
              weakMulticastDelegate.Remove(null);
              Assert.AreEqual(0, weakMulticastDelegate.Count);

              weakMulticastDelegate.Invoke(parameter); // Dummy invoke on empty WeakDelegateManager.
        }
示例#4
0
        /// <summary>
        /// Removes an event handler from the list
        /// </summary>
        /// <param name="e">The event from which to remove the handler</param>
        /// <param name="del">The callback method to remove</param>
        public void RemoveHandler(DOLEvent e, DOLEventHandler del)
        {
            if (_lock.TryEnterWriteLock(LOCK_TIMEOUT))
            {
                try
                {
                    WeakMulticastDelegate deleg;

                    if (_events.TryGetValue(e, out deleg))
                    {
                        deleg = WeakMulticastDelegate.Remove(deleg, del);

                        if (deleg == null)
                        {
                            _events.Remove(e);
                        }
                        else
                        {
                            _events[e] = deleg;
                        }
                    }
                }
                finally
                {
                    _lock.ExitWriteLock();
                }
            }
        }
示例#5
0
 public void RemoveHandler(RoadEvent e, RoadEventHandler del)
 {
     try
     {
         this.m_lock.AcquireWriterLock(3000);
         try
         {
             WeakMulticastDelegate deleg = (WeakMulticastDelegate)this.m_events[e];
             if (deleg != null)
             {
                 deleg = WeakMulticastDelegate.Remove(deleg, del);
                 if (deleg == null)
                 {
                     this.m_events.Remove(e);
                 }
                 else
                 {
                     this.m_events[e] = deleg;
                 }
             }
         }
         finally
         {
             this.m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException ex)
     {
         LogProvider.Default.Error("Failed to remove event handler!", ex);
     }
 }
示例#6
0
 /// <summary>
 /// Removes an event handler from the list
 /// </summary>
 /// <param name="e">The event from which to remove the handler</param>
 /// <param name="del">The callback method to remove</param>
 public void RemoveHandler(RoadEvent e, RoadEventHandler del)
 {
     try
     {
         m_lock.AcquireWriterLock(TIMEOUT);
         try
         {
             WeakMulticastDelegate deleg = (WeakMulticastDelegate)m_events[e];
             if (deleg != null)
             {
                 deleg = WeakMulticastDelegate.Remove(deleg, del);
                 if (deleg == null)
                 {
                     m_events.Remove(e);
                 }
                 else
                 {
                     m_events[e] = deleg;
                 }
             }
         }
         finally
         {
             m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException ex)
     {
         if (log.IsErrorEnabled)
         {
             log.Error("Failed to remove event handler!", ex);
         }
     }
 }
示例#7
0
 public void RemoveHandler(RoadEvent e, RoadEventHandler del)
 {
     try
     {
         this.m_lock.AcquireWriterLock(3000);
         try
         {
             WeakMulticastDelegate weakMulticastDelegate = (WeakMulticastDelegate)this.m_events[e];
             if (weakMulticastDelegate != null)
             {
                 weakMulticastDelegate = WeakMulticastDelegate.Remove(weakMulticastDelegate, del);
                 if (weakMulticastDelegate == null)
                 {
                     this.m_events.Remove(e);
                 }
                 else
                 {
                     this.m_events[e] = weakMulticastDelegate;
                 }
             }
         }
         finally
         {
             this.m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException exception)
     {
         if (RoadEventHandlerCollection.log.IsErrorEnabled)
         {
             RoadEventHandlerCollection.log.Error("Failed to remove event handler!", exception);
         }
     }
 }
        public void RemoveRegisteredDelegates()
        {
            MockDelegateTarget mockDelegateTarget1 = new MockDelegateTarget();
              MockDelegateTarget mockDelegateTarget2 = new MockDelegateTarget();

              var weakMulticastDelegate = new WeakMulticastDelegate<Action<object>>();
              weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod);
              weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod);
              weakMulticastDelegate.Add(mockDelegateTarget1.DelegateMethod2);
              weakMulticastDelegate.Add(mockDelegateTarget2.DelegateMethod2);
              weakMulticastDelegate.Add(MockStaticDelegateTarget.DelegateMethod);

              weakMulticastDelegate.Remove(mockDelegateTarget1.DelegateMethod);
              weakMulticastDelegate.Remove(mockDelegateTarget1.DelegateMethod);  // Remove twice
              weakMulticastDelegate.Remove(mockDelegateTarget2.DelegateMethod);
              weakMulticastDelegate.Remove(mockDelegateTarget2.DelegateMethod2);
              weakMulticastDelegate.Remove(MockStaticDelegateTarget.DelegateMethod);

              MockStaticDelegateTarget.Clear();
              object parameter = new object();
              weakMulticastDelegate.Invoke(parameter);

              Assert.IsFalse(mockDelegateTarget1.DelegateCalled);
              Assert.IsFalse(mockDelegateTarget2.DelegateCalled);
              Assert.IsTrue(mockDelegateTarget1.DelegateCalled2);
              Assert.IsFalse(mockDelegateTarget2.DelegateCalled2);
              Assert.IsFalse(MockStaticDelegateTarget.DelegateCalled);
              Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter2);
        }