コード例 #1
0
        internal static void Validate(string tagName, Object obj)
        {
#if DBG
            EnsureInit();

            if (obj != null &&
                (IsTagEnabled("Validate") ||
                 (!IsTagPresent("Validate") &&
                  (s_tableAlwaysValidate[tagName] != null ||
                   IsTagEnabled(tagName)))))
            {
                try {
                    Debug.Validate(obj);
                }
                catch (Exception e) {
                    Debug.Assert(false, "Validate failed: " + e.InnerException.Message);
                }
#pragma warning disable 1058
                catch {
                    Debug.Assert(false, "Validate failed.  Non-CLS compliant exception caught.");
                }
#pragma warning restore 1058
            }
#endif
        }
コード例 #2
0
        internal void StoreResource(IDisposable o)
        {
            lock (this) {
                Debug.Validate("ResourcePool", this);

                if (!_disposed)
                {
                    if (_resources.Count < _max)
                    {
                        _resources.Add(o);
                        o = null;
                        if (_timer == null)
                        {
#if DBG
                            if (!Debug.IsTagPresent("Timer") || Debug.IsTagEnabled("Timer"))
#endif
                            {
                                _timer = new Timer(_callback, null, _interval, _interval);
                            }
                        }
                    }

                    Debug.Validate("ResourcePool", this);
                }
            }

            if (o != null)
            {
                Debug.Trace("ResourcePool", "StoreResource reached max=" + _max);
                o.Dispose();
            }
        }
コード例 #3
0
        internal object RetrieveResource()
        {
            object result = null;

            // avoid lock in common case
            if (_resources.Count != 0)
            {
                lock (this) {
                    Debug.Validate("ResourcePool", this);

                    if (!_disposed)
                    {
                        if (_resources.Count == 0)
                        {
                            result = null;
                            Debug.Trace("ResourcePool", "RetrieveResource returned null");
                        }
                        else
                        {
                            result = _resources[_resources.Count - 1];
                            _resources.RemoveAt(_resources.Count - 1);
                            if (_resources.Count < _iDisposable)
                            {
                                _iDisposable = _resources.Count;
                            }
                        }

                        Debug.Validate("ResourcePool", this);
                    }
                }
            }

            return(result);
        }
コード例 #4
0
        internal ResourcePool(TimeSpan interval, int max)
        {
            _interval  = interval;
            _resources = new ArrayList(4);
            _max       = max;
            _callback  = new TimerCallback(this.TimerProc);

            Debug.Validate("ResourcePool", this);
        }
コード例 #5
0
        void TimerProc(Object userData)
        {
            IDisposable[] a = null;

            lock (this) {
                Debug.Validate("ResourcePool", this);

                if (!_disposed)
                {
                    if (_resources.Count == 0)
                    {
                        if (_timer != null)
                        {
                            _timer.Dispose();
                            _timer = null;
                        }

                        Debug.Validate("ResourcePool", this);
                        return;
                    }

                    a = new IDisposable[_iDisposable];
                    _resources.CopyTo(0, a, 0, _iDisposable);
                    _resources.RemoveRange(0, _iDisposable);

                    // It means that whatever remain in _resources will be disposed
                    // next time the timer proc is called.
                    _iDisposable = _resources.Count;

                    Debug.Trace("ResourcePool", "Timer disposing " + a.Length + "; remaining=" + _resources.Count);
                    Debug.Validate("ResourcePool", this);
                }
            }

            if (a != null)
            {
                for (int i = 0; i < a.Length; i++)
                {
                    try {
                        a[i].Dispose();
                    }
                    catch {
                        // ignore all errors
                    }
                }
            }
        }
コード例 #6
0
        public static void Validate(string tag, Object obj)
        {
#if DBG
            if (obj != null &&
                (IsTagEnabled("Validate") ||
                 (!IsTagPresent("Validate") &&
                  (s_tableAlwaysValidate[tag] != null ||
                   IsTagEnabled(tag)))))
            {
                try {
                    Debug.Validate(obj);
                }
                catch (Exception e) {
                    Debug.Assert(false, "Validate failed: " + e.InnerException.Message);
                }
            }
#endif
        }
コード例 #7
0
        void TimerProc(Object userData)
        {
            IDisposable[] a = null;

            lock (this) {
                Debug.Validate("ResourcePool", this);

                if (!_disposed)
                {
                    if (_resources.Count == 0)
                    {
                        if (_timer != null)
                        {
                            _timer.Dispose();
                            _timer = null;
                        }

                        Debug.Validate("ResourcePool", this);
                        return;
                    }

                    a = new IDisposable[_iDisposable];
                    _resources.CopyTo(0, a, 0, _iDisposable);
                    _resources.RemoveRange(0, _iDisposable);
                    _iDisposable = _resources.Count;

                    Debug.Trace("ResourcePool", "Timer disposing " + a.Length + "; remaining=" + _resources.Count);
                    Debug.Validate("ResourcePool", this);
                }
            }

            if (a != null)
            {
                for (int i = 0; i < a.Length; i++)
                {
                    a[i].Dispose();
                }
            }
        }