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.
        }
        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 InvokesRegisteredDelegates()
        {
            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);

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

              Assert.IsTrue(mockDelegateTarget1.DelegateCalled);
              Assert.IsTrue(mockDelegateTarget2.DelegateCalled);
              Assert.IsTrue(mockDelegateTarget1.DelegateCalled2);
              Assert.IsTrue(mockDelegateTarget2.DelegateCalled2);
              Assert.IsTrue(MockStaticDelegateTarget.DelegateCalled);
              Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter);
              Assert.AreSame(parameter, mockDelegateTarget2.DelegateParameter);
              Assert.AreSame(parameter, mockDelegateTarget1.DelegateParameter2);
              Assert.AreSame(parameter, mockDelegateTarget2.DelegateParameter2);
              Assert.AreSame(parameter, MockStaticDelegateTarget.DelegateParameter);
        }
Ejemplo n.º 5
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();
                }
            }
        }
Ejemplo n.º 6
0
 public void AddHandlerUnique(RoadEvent e, RoadEventHandler del)
 {
     try
     {
         this.m_lock.AcquireWriterLock(3000);
         try
         {
             WeakMulticastDelegate deleg = (WeakMulticastDelegate)this.m_events[e];
             if (deleg == null)
             {
                 this.m_events[e] = new WeakMulticastDelegate(del);
             }
             else
             {
                 this.m_events[e] = WeakMulticastDelegate.CombineUnique(deleg, del);
             }
         }
         finally
         {
             this.m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException ex)
     {
         LogProvider.Default.Error("Failed to add event handler!", ex);
     }
 }
Ejemplo n.º 7
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);
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Adds an event handler to the list, if it's already added do nothing
 /// </summary>
 /// <param name="e">The event from which we add a handler</param>
 /// <param name="del">The callback method</param>
 public void AddHandlerUnique(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.CombineUnique(deleg, del);
             }
         }
         finally
         {
             m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException ex)
     {
         if (log.IsErrorEnabled)
         {
             log.Error("Failed to add event handler!", ex);
         }
     }
 }
Ejemplo n.º 9
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);
         }
     }
 }
Ejemplo n.º 10
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);
         }
     }
 }
Ejemplo n.º 11
0
 public void AddHandlerUnique(RoadEvent e, RoadEventHandler del)
 {
     try
     {
         this.m_lock.AcquireWriterLock(3000);
         try
         {
             WeakMulticastDelegate weakMulticastDelegate = (WeakMulticastDelegate)this.m_events[e];
             if (weakMulticastDelegate == null)
             {
                 this.m_events[e] = new WeakMulticastDelegate(del);
             }
             else
             {
                 this.m_events[e] = WeakMulticastDelegate.CombineUnique(weakMulticastDelegate, del);
             }
         }
         finally
         {
             this.m_lock.ReleaseWriterLock();
         }
     }
     catch (ApplicationException exception)
     {
         if (RoadEventHandlerCollection.log.IsErrorEnabled)
         {
             RoadEventHandlerCollection.log.Error("Failed to add event handler!", exception);
         }
     }
 }
        protected SynchronizationContext(TrelloAuthorization auth, bool useTimer)
        {
            ManagesSubmissions = useTimer;
            if (useTimer && TrelloConfiguration.ChangeSubmissionTime.Milliseconds != 0)
            {
                _timer = new Timer(async state => await _TimerElapsed(), null,
                                   TrelloConfiguration.ChangeSubmissionTime,
                                   TrelloConfiguration.ChangeSubmissionTime);
            }

            _updateLock = new object();
            _expireLock = new object();
            _semaphore  = new SemaphoreSlim(1, 1);
            _expires    = DateTime.MinValue;
            RestRequestProcessor.LastCall += _TimerElapsed;
            Auth         = auth ?? TrelloAuthorization.Default;
            Synchronized = new WeakMulticastDelegate();
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Notifies all registered event handlers of the occurance of an event!
        /// </summary>
        /// <param name="e">The event that occured</param>
        /// <param name="sender">The sender of this event</param>
        /// <param name="eArgs">The event arguments</param>
        /// <remarks>Overwrite the EventArgs class to set own arguments</remarks>
        public void Notify(DOLEvent e, object sender, EventArgs eArgs)
        {
            WeakMulticastDelegate eventDelegate = null;

            if (_lock.TryEnterReadLock(LOCK_TIMEOUT))
            {
                try
                {
                    if (!_events.TryGetValue(e, out eventDelegate))
                    {
                        return;
                    }
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }

            eventDelegate.InvokeSafe(new[] { e, sender, eArgs });
        }
Ejemplo n.º 14
0
        /// <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(DOLEvent e, DOLEventHandler del)
        {
            if (_lock.TryEnterWriteLock(LOCK_TIMEOUT))
            {
                try
                {
                    WeakMulticastDelegate deleg;

                    if (!_events.TryGetValue(e, out deleg))
                    {
                        _events.Add(e, new WeakMulticastDelegate(del));
                    }
                    else
                    {
                        _events[e] = WeakMulticastDelegate.Combine(deleg, del);
                    }
                }
                finally
                {
                    _lock.ExitWriteLock();
                }
            }
        }
        public void AddDelegateShouldThrowExceptionWhenNull()
        {
            var weakMulticastDelegate = new WeakMulticastDelegate <Action>();

            weakMulticastDelegate.Add(null);
        }
 public void AddDelegateShouldThrowExceptionWhenNull()
 {
     var weakMulticastDelegate = new WeakMulticastDelegate<Action>();
       weakMulticastDelegate.Add(null);
 }