System.Runtime.InteropServices.HandleCollector.Add C# (CSharp) Method

Add() private method

private Add ( ) : void
return void
        public void Add () {
            int gen_collect = -1;
            Interlocked.Increment( ref handleCount);
            if( handleCount < 0) {
                throw new InvalidOperationException(SR.GetString(SR.InvalidOperation_HCCountOverflow));                 
            }

            if (handleCount > threshold) {
                lock (this) {
                    threshold = handleCount + (handleCount/deltaPercent);
                    gen_collect = gc_gen;
                    if (gc_gen < 2) {
                        gc_gen++;
                    }
                }                
            }

            if ((gen_collect >= 0) && 
                    ((gen_collect == 0) || 
                    (gc_counts[gen_collect] == GC.CollectionCount (gen_collect)))) {
                    GC.Collect (gen_collect);
                    Thread.Sleep (10*gen_collect);
            }

            //don't bother with gen0. 
            for (int i = 1; i < 3; i++) {
                gc_counts [i] = GC.CollectionCount (i);
            }
        }

Usage Example

コード例 #1
0
        public static void CountOverflow()
        {
            HandleCollector collector = new HandleCollector("CountOverflow", int.MaxValue);

            // We could iterate here 2B times calling Add, but that often takes over 100s
            // Instead, for testing purposes, we reach into the HandleCollector via reflection
            // to make it think it's already been called int.MaxValue - 10 times.  We then
            // only have to call Add 10 times rather than int.MaxValue times, and the test
            // completes very quickly.  If we ever need to run the test on a platform that
            // doesn't support such reflection, we can revert to the super-long running test
            // or find another workaround.

            const int ToAdd = 10; // int.MaxValue

            {
                // Jump HandleCollector instance forward until it almost overflows
                TypeInfo  type        = typeof(HandleCollector).GetTypeInfo();
                FieldInfo handleCount =
                    type.GetDeclaredField("_handleCount") ?? // corefx
                    type.GetDeclaredField("handleCount");    // desktop
                Assert.NotNull(handleCount);
                handleCount.SetValue(collector, int.MaxValue - ToAdd);
            }

            for (int i = 0; i < ToAdd; i++)
            {
                collector.Add();
            }

            Assert.Throws <InvalidOperationException>(() => collector.Add());
        }
All Usage Examples Of System.Runtime.InteropServices.HandleCollector::Add