Esempio n. 1
0
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            using (BlockReentrancy())
            {
                NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                if (eventHandler == null)
                {
                    return;
                }

                Delegate[] delegates = eventHandler.GetInvocationList();

                foreach (NotifyCollectionChangedEventHandler handler in delegates)
                {
                    DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
                    if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                    {
                        dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
                    }
                    else
                    {
                        handler(this, e);
                    }
                }
            }
        }
Esempio n. 2
0
        protected override void OnCollectionChanged(NotifyCollectionChangedEventHandler Handler, NotifyCollectionChangedEventArgs e)
        {
            if (e is null)
            {
                throw new ArgumentNullException(nameof(e));
            }
            if (Handler is null)
            {
                return;
            }
            var invocation_list = Handler.GetInvocationList();
            var args            = new object[] { this, e };

            foreach (var d in invocation_list)
            {
                var o = d.Target;
                switch (o)
                {
                case ISynchronizeInvoke {
                        InvokeRequired: true
                } synchronize_invoke:
                    synchronize_invoke.Invoke(d, args);
                    break;

                case DispatcherObject dispatcher_obj when !dispatcher_obj.CheckAccess() && dispatcher_obj.Dispatcher != null:
                    dispatcher_obj.Dispatcher.Invoke(d, args);
                    break;

                default:
                    d.DynamicInvoke(args);
                    break;
                }
            }
        }
        /// <summary>
        /// 重写集合发生变化事件处理函数。
        /// </summary>
        /// <param name="e">集合变化事件参数。</param>
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            // 不允许可重入的更改此集合的尝试。
            using (BlockReentrancy())
            {
                if (_collectionChanged != null)
                {
                    var delegates = _collectionChanged.GetInvocationList();

                    // 遍历调用列表。
                    foreach (var @delegate in delegates)
                    {
                        var handler = (NotifyCollectionChangedEventHandler)@delegate;
                        // 如果订阅者是DispatcherObject和不同的线程。
                        if (handler != null)
                        {
                            // 目标调度程序线程中的调用处理程序。
                            handler.Invoke(handler, e);
                        }
                        else // 按原样执行处理程序。
                        {
                            _collectionChanged(this, e);
                        }
                    }
                }
            }
        }
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!this.suspendCollectionChangeNotification)
            {
                NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                if (eventHandler != null)
                {
                    Delegate[] delegates      = eventHandler.GetInvocationList();
                    bool       isEventInvoked = false;
                    foreach (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        isEventInvoked = false;
                        if (handler.Target is DispatcherObject)
                        {
                            DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
                            if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                            {
                                dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, handler, this, e);
                                isEventInvoked = true;
                            }
                        }

                        if (!isEventInvoked)
                        {
                            handler(this, e);
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler collectionChanged = CollectionChanged;

            if (collectionChanged == null)
            {
                return;
            }

            foreach (Delegate @delegate in collectionChanged.GetInvocationList())
            {
                var        nh = (NotifyCollectionChangedEventHandler)@delegate;
                var        dispatcherObject = nh.Target as DispatcherObject;
                Dispatcher dispatcher       = dispatcherObject?.Dispatcher;
                if (dispatcher != null && !dispatcher.CheckAccess())
                {
                    dispatcher.BeginInvoke(DispatcherPriority.DataBind,
                                           (Action)(() => nh.Invoke(this,
                                                                    new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))));
                    continue;
                }

                nh.Invoke(this, e);
            }
        }
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            // Be nice - use BlockReentrancy like MSDN said
            using (BlockReentrancy())
            {
                if (_collectionChanged != null)
                {
                    Delegate[] delegates = _collectionChanged.GetInvocationList();

                    // Walk thru invocation list
                    foreach (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        var dispatcherObject = handler.Target as System.Windows.Forms.Control;

                        // If the subscriber is a DispatcherObject and different thread
                        if (dispatcherObject != null && dispatcherObject.InvokeRequired)
                        {
                            // Invoke handler in the target dispatcher's thread
                            dispatcherObject.Invoke(handler, this, e);
                        }
                        else // Execute handler as is
                        {
                            _collectionChanged(this, e);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Taken from http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/have-worker-thread-update-observablecollection-that-is-bound-to-a.aspx
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!_suspendCollectionChangeNotification)
            {
                // Be nice - use BlockReentrancy like MSDN said
                using (BlockReentrancy())
                {
                    NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                    if (eventHandler == null)
                    {
                        return;
                    }

                    Delegate[] delegates = eventHandler.GetInvocationList();
                    // Walk thru invocation list
                    foreach (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        var dispatcherObject = handler.Target as DispatcherObject;
                        // If the subscriber is a DispatcherObject and different thread
                        if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                        {
                            // Invoke handler in the target dispatcher's thread
                            dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, handler, this, e);
                        }
                        else                         // Execute handler as is
                        {
                            handler(this, e);
                        }
                    }
                }
            }
        }
Esempio n. 8
0
 public static void InvokeCollectionChanged(NotifyCollectionChangedEventHandler collectionChanged, object sender, NotifyCollectionChangedEventArgs e)
 {
     foreach (NotifyCollectionChangedEventHandler nh in collectionChanged.GetInvocationList())
     {
         var dispObj = nh.Target as DispatcherObject;
         if (dispObj != null)
         {
             Dispatcher dispatcher = dispObj.Dispatcher;
             if (dispatcher != null && !dispatcher.CheckAccess())
             {
                 NotifyCollectionChangedEventHandler nh1 = nh;
                 dispatcher.BeginInvoke((Action)(() => nh1.Invoke(sender, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
                                        DispatcherPriority.DataBind);
                 continue;
             }
         }
         if ((e.NewItems != null && e.NewItems.Count > 1) || (e.OldItems != null && e.OldItems.Count > 1))
         {
             nh.Invoke(sender, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
         }
         else
         {
             nh.Invoke(sender, e);
         }
     }
 }
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
        {
            NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;

            if (eventHandler != null)
            {
                Delegate[] delegates = eventHandler.GetInvocationList();
                // Walk thru invocation list
                foreach (NotifyCollectionChangedEventHandler handler in delegates)
                {
                    var dispatcherObject = handler.Target as DispatcherObject;
                    // If the subscriber is a DispatcherObject and different thread
                    if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                    {
                        // Invoke handler in the target dispatcher's thread
                        dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind,
                                                           handler, this, args);
                    }
                    else // Execute handler as is
                    {
                        handler(this, args);
                    }
                }
            }
        }
Esempio n. 10
0
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler CollectionChanged = this.CollectionChanged;

            if (CollectionChanged != null)
            {
                foreach (NotifyCollectionChangedEventHandler nh in CollectionChanged.GetInvocationList())
                {
                    DispatcherObject dispObj = nh.Target as DispatcherObject;
                    if (dispObj != null)
                    {
                        Dispatcher dispatcher = dispObj.Dispatcher;
                        if (dispatcher != null && !dispatcher.CheckAccess())
                        {
                            dispatcher.BeginInvoke(
                                (Action)(() => nh.Invoke(this,
                                                         new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
                                DispatcherPriority.DataBind);
                            continue;
                        }
                    }
                    nh.Invoke(this, e);
                }
            }
        }
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            // Be nice - use BlockReentrancy like MSDN said
            using (BlockReentrancy())
            {
                if (collectionChanged != null)
                {
                    Delegate[] delegates = collectionChanged.GetInvocationList();

                    // Walk thru invocation list
                    foreach (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        // If the subscriber is a DispatcherObject and different thread
                        if (handler != null)
                        {
                            // Invoke handler in the target dispatcher's thread
                            handler.Invoke(handler, e);
                        }
                        else // Execute handler as is
                        {
                            collectionChanged(this, e);
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        public static void ThreadSafeInvoke(
            [CanBeNull] this NotifyCollectionChangedEventHandler Handler,
            [CanBeNull] object Sender,
            [NotNull] NotifyCollectionChangedEventArgs E)
        {
            if (E is null)
            {
                throw new ArgumentNullException(nameof(E));
            }
            if (Handler is null)
            {
                return;
            }
            var invocation_list = Handler.GetInvocationList();
            var args            = new[] { Sender, E };

            foreach (var d in invocation_list)
            {
                var o = d.Target;
                switch (o)
                {
                case ISynchronizeInvoke synchronize_invoke when synchronize_invoke.InvokeRequired:
                    synchronize_invoke.Invoke(d, args);
                    break;

                case DispatcherObject dispatcher_obj when !dispatcher_obj.CheckAccess() && dispatcher_obj.Dispatcher != null:
                    dispatcher_obj.Dispatcher.Invoke(d, args);
                    break;

                default:
                    d.DynamicInvoke(args);
                    break;
                }
            }
        }
		protected void CheckReentrancy ()
		{
			NotifyCollectionChangedEventHandler eh = CollectionChanged;

			// Only have a problem if we have more than one event listener.
			if (reentrant.Busy && eh != null && eh.GetInvocationList ().Length > 1)
				throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
		}
Esempio n. 14
0
        /// <summary>
        /// Invokes the specified <paramref name="handler"/> in a thread-safe manner. Where normally one
        /// has to write the following code:
        /// <para />
        /// <code>
        /// <![CDATA[
        /// var handler = CollectionChanged;
        /// if (handler != null)
        /// {
        ///     handler(this, e, new NotifyCollectionChangedEventArgs(...));
        /// }
        /// ]]>
        /// </code>
        /// <para />
        /// One can now write:
        /// <para />
        /// <code>
        /// CollectionChanged.SafeInvoke(this, e, new NotifyCollectionChangedEventArgs(...));
        /// </code>
        /// </summary>
        /// <param name="handler">The handler.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args.</param>
        /// <returns><c>true</c> if the event handler was not <c>null</c>; otherwise <c>false</c>.</returns>
        public static bool SafeInvoke(this NotifyCollectionChangedEventHandler handler, object sender, NotifyCollectionChangedEventArgs e)
        {
            if (handler != null)
            {
                SplitInvoke <NotifyCollectionChangedEventHandler>(handler.GetInvocationList(), x => x(sender, e), sender, e);
                return(true);
            }

            return(false);
        }
    //CollectionViews raise an error when they are passed a NotifyCollectionChangedEventArgs that indicates more than
    //one element has been added or removed. They prefer to receive a "Action=Reset" notification, but this is not suitable
    //for applications in code, so we actually check the type we're notifying on and pass a customized event args.
    protected virtual void OnCollectionChangedMultiItem(NotifyCollectionChangedEventArgs e)
    {
        NotifyCollectionChangedEventHandler handlers = this.CollectionChanged;

        if (handlers != null)
        {
            foreach (NotifyCollectionChangedEventHandler handler in handlers.GetInvocationList())
            {
                handler(this, !(handler.Target is ICollectionView) ? e : new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    }
Esempio n. 16
0
 public static IEnumerable <T> GetHandlers <T>(NotifyCollectionChangedEventHandler handler)
 {
     if (handler == null)
     {
         yield break;
     }
     foreach (var invocator in handler.GetInvocationList())
     {
         if (invocator.Target is T container)
         {
             yield return(container);
         }
     }
 }
Esempio n. 17
0
        /// <summary>
        /// This collection changed event performs thread safe event raising.
        /// </summary>
        /// <param name="e">The event argument.</param>
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            // Recommended is to avoid reentry
            // in collection changed event while collection
            // is getting changed on other thread.
            using (BlockReentrancy())
            {
                if (!suspendCollectionChangeNotification)
                {
                    NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                    if (eventHandler == null)
                    {
                        return;
                    }

                    // Walk thru invocation list.
                    Delegate[] delegates = eventHandler.GetInvocationList();

                    foreach
                    (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        // If the subscriber is a DispatcherObject and different thread.
                        DispatcherObject dispatcherObject
                            = handler.Target as DispatcherObject;

                        if (dispatcherObject != null)
                        {
                            if (!dispatcherObject.CheckAccess())
                            {
                                // Invoke handler in the target dispatcher's thread...
                                // asynchronously for better responsiveness.
                                dispatcherObject.Dispatcher.BeginInvoke
                                    (DispatcherPriority.DataBind, handler, this, e);
                            }
                            else
                            {
                                // Execute handler as is.
                                handler(this, e);
                            }
                        }
                        else
                        {
                            // Execute handler as is.
                            handler(this, e);
                        }
                    }
                }
            }
        }
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
        {
            NotifyCollectionChangedEventHandler notifyCollectionChangedEventHandler = CollectionChanged;

            if (notifyCollectionChangedEventHandler == null)
            {
                return;
            }

            foreach (NotifyCollectionChangedEventHandler handler in notifyCollectionChangedEventHandler.GetInvocationList())
            {
                if (handler.Target is DispatcherObject dispatcherObject && !dispatcherObject.CheckAccess())
                {
                    dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, args);
                }
        public bool RemoveCollectionChangedSubscribersAll()
        {
            NotifyCollectionChangedEventHandler _event = CollectionChanged;

            if (_event == null)  // No subscriber
            {
                return(false);
            }

            // Go through handler
            foreach (NotifyCollectionChangedEventHandler handler in _event.GetInvocationList())
            {
                CollectionChanged -= handler;
            }

            return(true);
        }
Esempio n. 20
0
 public static void Start(
     [CanBeNull] this NotifyCollectionChangedEventHandler Handler,
     object Sender,
     NotifyCollectionChangedEventArgs e)
 {
     if (Handler == null)
     {
         return;
     }
     foreach (Delegate invocation in Handler.GetInvocationList())
     {
         if (invocation.Target is ISynchronizeInvoke target && target.InvokeRequired)
         {
             target.Invoke(invocation, new object[2]
             {
                 Sender,
                 (object)e
             });
         }
        protected virtual void OnCollectionChangedMultiItem(NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler handlers = CollectionChanged;

            if (handlers != null)
            {
                foreach (NotifyCollectionChangedEventHandler handler in handlers.GetInvocationList())
                {
                    if (handler.Target is CollectionView)
                    {
                        ((CollectionView)handler.Target).Refresh();
                    }
                    else
                    {
                        handler(this, e);
                    }
                }
            }
        }
Esempio n. 22
0
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            // Be nice - use BlockReentrancy like MSDN said
            using (BlockReentrancy())
            {
                NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
                if (eventHandler == null)
                {
                    return;
                }

                Delegate[] delegates = eventHandler.GetInvocationList();
                // Walk thru invocation list
                foreach (NotifyCollectionChangedEventHandler handler in delegates.Cast <NotifyCollectionChangedEventHandler>())
                {
                    NotifyCollectionChangedEventHandler handler1 = handler;
                    _dispatcherInvoker.Invoke(() => handler1(this, e));
                }
            }
        }
Esempio n. 23
0
        public static void InvokeCollectionChanged(NotifyCollectionChangedEventHandler collectionChanged, object sender, NotifyCollectionChangedEventArgs e)
        {
            foreach (NotifyCollectionChangedEventHandler nh in collectionChanged.GetInvocationList())
            {
                var dispObj = nh.Target as DispatcherObject;
                if (dispObj != null)
                {
                    Dispatcher dispatcher = dispObj.Dispatcher;
                    if (dispatcher != null && !dispatcher.CheckAccess())
                    {
                        NotifyCollectionChangedEventHandler nh1 = nh;
                        dispatcher.BeginInvoke((Action) (() => nh1.Invoke(sender, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
                            DispatcherPriority.DataBind);
                        continue;
                    }
                }

                nh.Invoke(sender, IsRangeAction(e) ? new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset) : e);
            }
        }
Esempio n. 24
0
 private void OnCollectionChanged(NotifyCollectionChangedEventHandler eventHandler, NotifyCollectionChangedEventArgs e)
 {
     // Walk through invocation list.
     Delegate[] delegates = eventHandler.GetInvocationList();
     foreach (NotifyCollectionChangedEventHandler handler in delegates)
     {
         // If the subscriber is a DispatcherObject and different thread.
         var dispatcherObject = handler.Target as DispatcherObject;
         if (dispatcherObject != null && !dispatcherObject.CheckAccess())
         {
             // Invoke handler in the target dispatcher's thread...
             // asynchronously for better responsiveness.
             dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, handler, this, e);
         }
         else
         {
             // Execute handler as is.
             handler(this, e);
         }
     }
 }
        /// <summary>
        /// Invokes the change event on the dispatcher if needed.
        /// </summary>
        /// <param name="handler">The NotifyCollectionChangedEventHandler.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/>.</param>
        public static Task InvokeOnDispatcherAsync(this NotifyCollectionChangedEventHandler handler, object sender, NotifyCollectionChangedEventArgs e)
        {
            if (handler == null)
            {
                return(FinishedTask);
            }

            var invocationList = handler.GetInvocationList();

            if (invocationList.Length == 0)
            {
                return(FinishedTask);
            }

            if (invocationList.Length == 1)
            {
                return(NotifyAsync(sender, e, invocationList[0]));
            }

            return(Task.WhenAll(invocationList.Select(x => NotifyAsync(sender, e, x))));
        }
        public bool RemoveCollectionChangedSubscriber(object subscriber)
        {
            NotifyCollectionChangedEventHandler _event = CollectionChanged;

            if (_event == null)  // No subscriber
            {
                return(false);
            }

            // Go through handler
            foreach (NotifyCollectionChangedEventHandler handler in _event.GetInvocationList())
            {
                if (object.ReferenceEquals(handler.Target, subscriber))
                {
                    CollectionChanged -= handler;
                    return(true);
                }
            }

            return(false);
        }
        public static void InvokeOnDispatcher(this NotifyCollectionChangedEventHandler handler, object sender, NotifyCollectionChangedEventArgs e)
        {
            if (handler == null)
            {
                return;
            }
            var invocationList = handler.GetInvocationList().OfType <NotifyCollectionChangedEventHandler>();

            foreach (var invocation in invocationList)
            {
                var dispatcherObject = invocation.Target as DispatcherObject;

                if (dispatcherObject != null && !dispatcherObject.CheckAccess())
                {
                    dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, invocation, sender, e);
                }
                else
                {
                    invocation(sender, e); // note : this does not execute invocation in target thread's context
                }
            }
        }
Esempio n. 28
0
        public void Invoke(NotifyCollectionChangedEventHandler eventHandler, object source, NotifyCollectionChangedEventArgs args)
        {
            if (eventHandler == null)
            {
                return;
            }
            var delegates = eventHandler.GetInvocationList();

            async void BeginInvoke(Delegate d)
            {
                //Debug.Print(args.ToString());
                switch (d.Target)
                {
                case DispatcherObject dispatcherObject:
                    await dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, d, source, args);

                    break;

                case ISynchronizeInvoke s:
                    s.BeginInvoke(d, new[] { source, args });
                    break;

                default:
                    d.Method.Invoke(d.Target, new object[] { source, args });
                    //Application.Current?.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, d, source, args);
                    //var t = new Task(() => d.Method.Invoke(d.Target, new object[] {source, args}));
                    //t.Start();
                    //await t;
                    //d.DynamicInvoke(source, args); // note : this does not execute handler in target thread's context
                    break;
                }
            }

            foreach (var d in delegates)
            {
                BeginInvoke(d);
            }
        }
Esempio n. 29
0
        /// <summary>
        /// 必要ならGUIスレッド上でCollectionChangedを呼び出します。
        /// </summary>
        public static void CallCollectionChanged(NotifyCollectionChangedEventHandler handler,
                                                 object sender,
                                                 NotifyCollectionChangedEventArgs e)
        {
            if (handler == null)
            {
                return;
            }

            // 個々のDelegate単位で呼び出すスレッドを変更します。
            foreach (NotifyCollectionChangedEventHandler child in
                     handler.GetInvocationList())
            {
                var target = child.Target as DispatcherObject;

                try
                {
                    // 必要があれば指定のスレッド上で実行します。
                    if (target != null && !target.Dispatcher.CheckAccess())
                    {
                        // コレクションの状態が変わる前に変更通知を出す
                        // 必要があるため、Invokeを使っています。
                        target.Dispatcher.Invoke(
                            child,
                            sender, e);
                    }
                    else
                    {
                        child(sender, e);
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorException(ex,
                                       "CollectionChangedの呼び出しに失敗しました。");
                }
            }
        }
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (var item in e.NewItems)
                {
                    (item as INotifyPropertyChanged).PropertyChanged += MyObservableCollection_PropertyChanged;
                }
            }
            if (e.OldItems != null)
            {
                foreach (var item in e.OldItems)
                {
                    (item as INotifyPropertyChanged).PropertyChanged -= MyObservableCollection_PropertyChanged;
                }
            }
            using (BlockReentrancy())
            {
                if (!this.suspendCollectionChangeNotification)
                {
                    NotifyCollectionChangedEventHandler eventHandler =
                        this.CollectionChanged;
                    if (eventHandler == null)
                    {
                        return;
                    }

                    // Walk thru invocation list.
                    Delegate[] delegates = eventHandler.GetInvocationList();

                    foreach
                    (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        handler(this, e);
                    }
                }
            }
        }
Esempio n. 31
0
        /// <summary>
        /// Provides a thread-safe means of notfying all subscribers.
        /// </summary>
        /// <param name="e">The event args to send.</param>
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            lock (_onCollectionChangedLock)
            {
                NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;

                if (eventHandler != null)
                {
                    Delegate[] delegates = eventHandler.GetInvocationList();

                    foreach (NotifyCollectionChangedEventHandler handler in delegates)
                    {
                        // Check if we need to invoke the handler on a different thread.
                        if (handler.Target is DispatcherObject dispatcherObject && dispatcherObject.CheckAccess() == false)
                        {
                            dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
                        }
                        else
                        {
                            handler(this, e);
                        }
                    }
                }