Exemplo n.º 1
0
        /// <summary>
        ///     Dispose all un-released items in the poll storage and the pool itself
        /// </summary>
        public void Dispose()
        {
            if (IsDisposed)
            {
                return;
            }

            IsDisposed = true;
            // Do we have items in the store and does the item support the IDisposable interface?
            if (ItemStore.Count > 0 && typeof(IDisposable).IsAssignableFrom(typeof(T)))
            {
                // If so, try to dispose all unreleased items from the storage
                lock (ItemStore) {
                    do
                    {
                        var disposable = (IDisposable)ItemStore.Fetch();
                        // Avoid expensive exceptions
                        if (disposable == null)
                        {
                            continue;
                        }

                        try {
                            disposable.Dispose();
                        } catch {
                        }
                    } while (ItemStore.Count > 0);
                }
            }

            // Close the semaphore
            _sync.Close();
        }
Exemplo n.º 2
0
 public override T GetItem()
 {
     if (ItemStore.Count > 0)
     {
         return(ItemStore.Fetch());
     }
     return(factory());
 }
Exemplo n.º 3
0
        public override T Acquire()
        {
            _sync.WaitOne();

            lock (ItemStore) {
                return(ItemStore.Fetch());
            }
        }
Exemplo n.º 4
0
        public override T Acquire()
        {
            _sync.WaitOne();

            // Try to get it from the pool of already released items
            lock (ItemStore) {
                if (ItemStore.Count > 0)
                {
                    return(ItemStore.Fetch());
                }
            }

            // Raise amount of lazy-loaded items
            var tmp = LazyLoadedItemCount;

            Interlocked.Increment(ref tmp);
            LazyLoadedItemCount = tmp;
            // Fetch a new item
            return(ItemFactoryFunc(this));
        }
Exemplo n.º 5
0
 public override T GetItem()
 {
     return(ItemStore.Fetch());
 }