Beispiel #1
0
        /// <summary>
        ///     Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing">
        ///     <c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.
        /// </param>
        /// <returns></returns>
        protected override bool Dispose(bool disposing)
        {
            if (!disposing)
            {
                return(false);
            }

            _isActive = false;

            if (_canExecuteMethod != null)
            {
                _canExecuteMethod.Dispose();
                _canExecuteMethod = null;
            }

            if (_executeMethod != null)
            {
                _executeMethod.Dispose();
                _executeMethod = null;
            }

            if (_isActiveChangedHandlers != null)
            {
                _isActiveChangedHandlers.Dispose();
                _isActiveChangedHandlers = null;
            }

            if (_canExecuteChangedHandlers != null)
            {
                _canExecuteChangedHandlers.Dispose();
                _canExecuteChangedHandlers = null;
            }
            return(base.Dispose(true));
        }
        public static void RegisterWeakDelegate(IWeakDelegate weak)
        {
            // If this is not an instance delegate we haven nothing to register
            if (weak.Reference.Target == null)
            {
                return;
            }

            bool startThread = false;

            // Add a reference to our instance delegate
            lock (InstanceDelegates) {
                if (!InstanceDelegates.ContainsKey(weak))
                {
                    InstanceDelegates.Add(weak, weak);
                    startThread = (InstanceDelegates.Count == 1 && CollectionThread == null);
                    if (startThread)
                    {
                        CollectionThread = new Thread(RunCollectionThread);
                    }
                }
            }

            // If we need to start our garbage collection thread then start it up
            if (startThread)
            {
                CollectionThread.IsBackground = true;
                CollectionThread.Start();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initialize with specified execute methods.
        /// </summary>
        /// <param name="executeMethod">The execute method.</param>
        /// <param name="canExecuteMethod">The can execute method.</param>
        /// <exception cref="System.ArgumentNullException">executeMethod</exception>
        internal protected virtual void Init(Action <object> executeMethod, Func <object, bool> canExecuteMethod)
        {
            if (executeMethod == null || canExecuteMethod == null)
            {
                throw new ArgumentNullException("executeMethod", Properties.Resources.ErrMsg_CommandDelegatesCantBeNull);
            }

            _executeMethod    = new DelegateReference(executeMethod, true);
            _canExecuteMethod = new DelegateReference(canExecuteMethod, true);
        }
Beispiel #4
0
        /// <summary>
        /// Initialize with specified execute method.
        /// </summary>
        /// <param name="executeMethod">The execute method.</param>
        /// <exception cref="System.ArgumentNullException">executeMethod</exception>
        internal protected virtual void Init(Action <object> executeMethod)
        {
            if (executeMethod == null)
            {
                //System.Diagnostics.Debug.WriteLine(string.Format("[{0}] executeMethod == null !!!", typeof(DelegateCommand)));
                throw new ArgumentNullException("executeMethod", Properties.Resources.ErrMsg_CommandDelegatesCantBeNull);
            }

            _executeMethod = new DelegateReference(executeMethod, true);
            //System.Diagnostics.Debug.WriteLine(string.Format("[{0}] _executeMethod == {1} !!!", typeof(DelegateCommand), _executeMethod));

            Func <object, bool> func = o => CanExecuteCommand(this);

            _canExecuteMethod = new DelegateReference(func, true);
            //System.Diagnostics.Debug.WriteLine(string.Format("[{0}] _canExecuteMethod == {1} !!!", typeof(DelegateCommand), _canExecuteMethod));
        }
Beispiel #5
0
        public void TestDisposeWithMethod()
        {
            IWeakDelegate weakDel = null;

            new Action(() =>
            {
                Subscriber sub = new Subscriber();

                weakDel = new WeakDelegate(sub, sub, ((Action <bool>)sub.Listener).GetMethodInfo());

                Assert.IsTrue(weakDel.IsSubscriberAlive);

                sub = null;
            })();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Assert.IsFalse(weakDel.IsSubscriberAlive);
        }
 public XChannel(ChannelIdentification channelIdentification, object?targetId, bool exclusiveChannel, IWeakDelegate weakDelegate)
 {
     this.Identification   = channelIdentification;
     this.TargetId         = targetId;
     this.ExclusiveChannel = exclusiveChannel;
     this.WeakDelegate     = weakDelegate;
 }
        private static XChannel AddXChannel(LinkedList <XChannel> list, ChannelIdentification identification, object?targetId, bool exclusiveChannel, IWeakDelegate weakDelegate)
        { // lock (cs) required.
            if (exclusiveChannel)
            {
                if (list.Count > 0)
                { // other channel already exists.
                    throw new InvalidOperationException();
                }
            }
            else
            {
                if (list.First?.Value.ExclusiveChannel == true)
                { // Exclusive channel exists.
                    throw new InvalidOperationException();
                }
            }

            // New XChannel
            var channel = new XChannel(identification, targetId, exclusiveChannel, weakDelegate);

            // list: Identification to XChannels.
            channel.Node = list.AddLast(channel);

            return(channel);
        }
Beispiel #8
0
 private bool Subscribe(int key, IWeakDelegate weakDel)
 {
     return(GetSubscriberSet(key).Add(weakDel));
 }
 public static void UnregisterWeakDelegate(IWeakDelegate weak)
 {
     lock (InstanceDelegates) {
         InstanceDelegates.Remove(weak);
     }
 }