Beispiel #1
0
        private void InitDisposableMembers(NameValueCollection config)
        {
            bool dispose = true;

            try {
                try {
                    _perfCounters = new PerfCounters(_name);
                }
                catch {
                    // ignore exceptions from perf counters
                }
                for (int i = 0; i < _storeCount; i++)
                {
                    _storeRefs[i] = new GCHandleRef <MemoryCacheStore> (new MemoryCacheStore(this, _perfCounters));
                }
                _stats = new MemoryCacheStatistics(this, config);
                AppDomain    appDomain         = Thread.GetDomain();
                EventHandler onAppDomainUnload = new EventHandler(OnAppDomainUnload);
                appDomain.DomainUnload += onAppDomainUnload;
                _onAppDomainUnload      = onAppDomainUnload;
                UnhandledExceptionEventHandler onUnhandledException = new UnhandledExceptionEventHandler(OnUnhandledException);
                appDomain.UnhandledException += onUnhandledException;
                _onUnhandledException         = onUnhandledException;
                dispose = false;
            }
            finally {
                if (dispose)
                {
                    Dispose();
                }
            }
        }
Beispiel #2
0
        internal void EnableExpirationTimer(bool enable)
        {
            if (enable)
            {
                if (_timerHandleRef == null)
                {
                    DateTime utcNow = DateTime.UtcNow;
                    TimeSpan due    = _tsPerBucket - (new TimeSpan(utcNow.Ticks % _tsPerBucket.Ticks));
                    Timer    timer  = new Timer(new TimerCallback(this.TimerCallback), null,
                                                due.Ticks / TimeSpan.TicksPerMillisecond, _tsPerBucket.Ticks / TimeSpan.TicksPerMillisecond);
                    _timerHandleRef = new GCHandleRef <Timer>(timer);

                    Dbg.Trace("Cache", "Cache expiration timer created.");
                }
            }
            else
            {
                GCHandleRef <Timer> timerHandleRef = _timerHandleRef;
                if (timerHandleRef != null && Interlocked.CompareExchange(ref _timerHandleRef, null, timerHandleRef) == timerHandleRef)
                {
                    timerHandleRef.Dispose();

                    Dbg.Trace("Cache", "Cache expiration timer disposed.");
                    while (_inFlush != 0)
                    {
                        Thread.Sleep(100);
                    }
                }
            }
        }
Beispiel #3
0
        internal void EnableExpirationTimer(bool enable)
        {
            if (enable)
            {
                if (_timerHandleRef == null)
                {
                    DateTime utcNow = DateTime.UtcNow;
                    TimeSpan due    = _tsPerBucket - (new TimeSpan(utcNow.Ticks % _tsPerBucket.Ticks));
                    Timer    timer;
                    // Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever
                    bool restoreFlow = false;
                    try
                    {
                        if (!ExecutionContext.IsFlowSuppressed())
                        {
                            ExecutionContext.SuppressFlow();
                            restoreFlow = true;
                        }

                        timer = new Timer(new TimerCallback(this.TimerCallback), null,
                                          due.Ticks / TimeSpan.TicksPerMillisecond, _tsPerBucket.Ticks / TimeSpan.TicksPerMillisecond);
                    }
                    finally
                    {
                        // Restore the current ExecutionContext
                        if (restoreFlow)
                        {
                            ExecutionContext.RestoreFlow();
                        }
                    }
                    _timerHandleRef = new GCHandleRef <Timer>(timer);

                    Dbg.Trace("Cache", "Cache expiration timer created.");
                }
            }
            else
            {
                GCHandleRef <Timer> timerHandleRef = _timerHandleRef;
                if (timerHandleRef != null && Interlocked.CompareExchange(ref _timerHandleRef, null, timerHandleRef) == timerHandleRef)
                {
                    timerHandleRef.Dispose();

                    Dbg.Trace("Cache", "Cache expiration timer disposed.");
                    while (_inFlush != 0)
                    {
                        Thread.Sleep(100);
                    }
                }
            }
        }
Beispiel #4
0
        private void InitDisposableMembers()
        {
            bool dispose = true;

            try {
                _cacheMemoryMonitor = new CacheMemoryMonitor(_memoryCache, _configCacheMemoryLimitMegabytes);
                Timer timer = new Timer(new TimerCallback(CacheManagerTimerCallback), null, _configPollingInterval, _configPollingInterval);
                _timerHandleRef = new GCHandleRef <Timer>(timer);
                dispose         = false;
            }
            finally {
                if (dispose)
                {
                    Dispose();
                }
            }
        }
 public void Dispose()
 {
     if (Interlocked.Exchange(ref _disposed, 1) == 0)
     {
         lock (_timerLock)
         {
             GCHandleRef <Timer> timerHandleRef = _timerHandleRef;
             if (timerHandleRef != null && Interlocked.CompareExchange(ref _timerHandleRef, null, timerHandleRef) == timerHandleRef)
             {
                 timerHandleRef.Dispose();
                 Dbg.Trace("MemoryCacheStats", "Stopped CacheMemoryTimers");
             }
         }
         while (_inCacheManagerThread != 0)
         {
             Thread.Sleep(100);
         }
         _cacheMemoryMonitor?.Dispose();
         // Don't need to call GC.SuppressFinalize(this) for sealed types without finalizers.
     }
 }
Beispiel #6
0
        private void InitDisposableMembers()
        {
            bool dispose = true;

            try
            {
                _cacheMemoryMonitor = new CacheMemoryMonitor(_memoryCache, _configCacheMemoryLimitMegabytes);
                Timer timer;
                // Don't capture the current ExecutionContext and its AsyncLocals onto the timer causing them to live forever
                bool restoreFlow = false;
                try
                {
                    if (!ExecutionContext.IsFlowSuppressed())
                    {
                        ExecutionContext.SuppressFlow();
                        restoreFlow = true;
                    }

                    timer = new Timer(new TimerCallback(CacheManagerTimerCallback), null, _configPollingInterval, _configPollingInterval);
                }
                finally
                {
                    // Restore the current ExecutionContext
                    if (restoreFlow)
                    {
                        ExecutionContext.RestoreFlow();
                    }
                }

                _timerHandleRef = new GCHandleRef <Timer>(timer);
                dispose         = false;
            }
            finally
            {
                if (dispose)
                {
                    Dispose();
                }
            }
        }