예제 #1
0
        public void Add()
        {
            var MyCollection = new WeakHashSet <object>();
            var MyObject     = new object();

            MyCollection.Add(MyObject);

            GC.Collect();

            Assert.AreEqual(1, MyCollection.Count(), "Item was not added");

            GC.KeepAlive(MyObject);
        }
예제 #2
0
        //****************************************

        private void AllocateRange <TItem>(out WeakHashSet <TItem> collection, int count) where TItem : class, new()
        {         //****************************************
            var Items = new TItem[count];

            //****************************************

            for (var Index = 0; Index < count; Index++)
            {
                Items[Index] = new TItem();
            }

            collection = new WeakHashSet <TItem>();
            collection.UnionWith(Items);
        }
예제 #3
0
파일: TestLock.cs 프로젝트: e2wugui/zeze
        public void Test()
        {
            // DEBUG 下垃圾回收策略导致 WeakReference 不回收。
#if RELEASE
            WeakHashSet <demo.Module1.Key> keys = new WeakHashSet <demo.Module1.Key>();
            demo.Module1.Key key1 = new demo.Module1.Key(1);
            demo.Module1.Key key2 = new demo.Module1.Key(1);

            Assert.IsTrue(null == keys.get(key1));
            keys.add(key1);

            demo.Module1.Key exist1 = keys.get(key1);
            Assert.IsTrue(null != exist1);
            Assert.IsTrue(exist1 == key1);

            demo.Module1.Key exist2 = keys.get(key2);
            Assert.IsTrue(null != exist2);
            Assert.IsTrue(exist2 == key1);

            key1   = null;
            key2   = null;
            exist1 = null;
            exist2 = null;

            demo.Module1.Key k4 = new demo.Module1.Key(1);
            WeakReference <demo.Module1.Key> wref = new WeakReference <demo.Module1.Key>(k4);
            k4 = null;
            for (int i = 0; i < 10; ++i)
            {
                System.GC.Collect();
                GC.WaitForFullGCComplete();
                GC.WaitForPendingFinalizers();
                Thread.Sleep(200);

                if (false == wref.TryGetTarget(out var notusedk4ref))
                {
                    break;
                }
            }

            demo.Module1.Key k4ref;
            Assert.IsTrue(false == wref.TryGetTarget(out k4ref));
            Assert.IsTrue(null == k4ref);

            demo.Module1.Key key3 = new demo.Module1.Key(1);
            Console.WriteLine("test: is null.");
            Assert.IsTrue(null == keys.get(key3));
#endif
        }
예제 #4
0
        public void AddRange()
        {
            var MyCollection = new WeakHashSet <object>();
            var MyObject1    = new object();
            var MyObject2    = new object();

            MyCollection.UnionWith(new object[] { MyObject1, MyObject2 });

            GC.Collect();

            Assert.AreEqual(2, MyCollection.Count(), "Items were not added");

            GC.KeepAlive(MyObject1);
            GC.KeepAlive(MyObject2);
        }
예제 #5
0
        private void AllocateWithDual <TItem>(out WeakHashSet <TItem> collection, out TItem item1, out TItem item2, int count) where TItem : class, new()
        {         //****************************************
            var Items = new TItem[count];

            //****************************************

            for (var Index = 0; Index < count; Index++)
            {
                Items[Index] = new TItem();
            }

            collection = new WeakHashSet <TItem>();

            collection.UnionWith(Items);
            collection.Add(item1 = new TItem());
            collection.Add(item2 = new TItem());
        }
예제 #6
0
        public void Cleanup()
        {
            WeakReference        WeakRef;
            WeakHashSet <object> WeakCollection;

            {
                var StrongRef = new object();
                WeakRef        = new WeakReference(StrongRef);
                WeakCollection = new WeakHashSet <object>(EqualityComparer <object> .Default)
                {
                    StrongRef
                };

                CollectionAssert.Contains(WeakCollection, StrongRef);
            }

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

            Assert.IsNull(WeakRef.Target);
        }
예제 #7
0
 /// <summary>
 ///     Method which generates keytips for elements provided by
 ///     KeyTipAutoGenerationElements enumerables in current scope.
 /// </summary>
 private void AutoGenerateKeyTips(WeakHashSet<DependencyObject> currentScopeElements)
 {
     if (currentScopeElements != null && currentScopeElements.Count > 0)
     {
         bool reprocessScoping = false;
         foreach (DependencyObject element in currentScopeElements)
         {
             IEnumerable<DependencyObject> keyTipAutoGenerationElements = GetKeyTipAutoGenerationElements(element);
             if (keyTipAutoGenerationElements != null)
             {
                 foreach (DependencyObject autoGenerationElement in keyTipAutoGenerationElements)
                 {
                     if (!_keyTipAutoGeneratedElements.Contains(autoGenerationElement))
                     {
                         reprocessScoping = true;
                         SetKeyTip(autoGenerationElement, GetNextAutoGenerationKeyTip(element));
                         _keyTipAutoGeneratedElements.Add(autoGenerationElement);
                     }
                 }
             }
         }
         if (reprocessScoping)
         {
             ProcessScoping();
         }
     }
 }
예제 #8
0
 private bool Delete(Computed dependent)
 {
     lock (this)
     {
         if (_computedArray != null)
         {
             WeakArray.Remove(ref _computedArray, dependent);
             return _computedArray == null;
         }
         else if (_computedHash != null)
         {
             _computedHash.Remove(dependent);
             if (_computedHash.Count == 0)
             {
                 _computedHash = null;
                 return true;
             }
             return false;
         }
         else
             return false;
     }
 }
예제 #9
0
 private bool Insert(Computed update)
 {
     lock (this)
     {
         if (_computedHash != null)
         {
             _computedHash.Add(update);
             return false;
         }
         if (WeakArray.Contains(ref _computedArray, update))
             return false;
         bool first = _computedArray == null;
         if (WeakArray.GetCount(ref _computedArray) >= _maxArraySize)
         {
             _computedHash = new WeakHashSet<Computed>();
             foreach (var item in WeakArray.Enumerate<Computed>(_computedArray))
                 _computedHash.Add(item);
             _computedArray = null;
             _computedHash.Add(update);
             return false;
         }
         WeakArray.Add(ref _computedArray, update);
         return first;
     }
 }