internal static bool Contains(ProducerConsumerQueue <T> target, T item)
 {
     Monitor.Enter(target.SyncRoot);
     try
     {
         for (QueueItem q = target._firstElement; q != null; q = q._followingElement)
         {
             if (target._areEqual(q._value, item))
             {
                 return(true);
             }
         }
     }
     finally { Monitor.Exit(target.SyncRoot); }
     return(false);
 }
            internal static bool Remove(ProducerConsumerQueue <T> target, T item)
            {
                bool collectionChanged = false;

                Monitor.Enter(target.SyncRoot);
                try
                {
                    for (QueueItem q = target._firstElement; q != null; q = q._followingElement)
                    {
                        if (target._areEqual(q._value, item))
                        {
                            item = q._value;
                            target.Count--;
                            collectionChanged = true;
                            bool wasFull = target.Count == target.BoundedCapacity;
                            if (q._precedingElement == null)
                            {
                                if (q._followingElement == null)
                                {
                                    target._firstElement = target._lastElement = null;
                                    target._notEmptyEvent.Reset();
                                }
                                else
                                {
                                    (target._firstElement = q._followingElement)._precedingElement = null;
                                }
                            }
                            else if (q._followingElement == null)
                            {
                                (target._lastElement = q._precedingElement)._followingElement = null;
                            }
                            else
                            {
                                q._precedingElement._followingElement = q._followingElement;
                                q._followingElement._precedingElement = q._precedingElement;
                            }
                            break;
                        }
                    }
                }
                finally { Monitor.Exit(target.SyncRoot); }
                if (collectionChanged)
                {
                    target.RaiseItemRemoved(0, item);
                }
                return(collectionChanged);
            }
 private void Target_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
 {
     if (_isTaking && e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null && e.OldItems.Count == 1 && _target._areEqual(_current, (T)(e.OldItems[0])))
     {
         _isTaking = false;
     }
     else
     {
         _collectionUnchanged = false;
     }
 }