/// <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(); }
public override T GetItem() { if (ItemStore.Count > 0) { return(ItemStore.Fetch()); } return(factory()); }
public override T Acquire() { _sync.WaitOne(); lock (ItemStore) { return(ItemStore.Fetch()); } }
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)); }
public override T GetItem() { return(ItemStore.Fetch()); }