SynchronizedDisposablePool.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / SynchronizedDisposablePool.cs / 1 / SynchronizedDisposablePool.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel
{ 
    using System.Collections.Generic;
    using System.Threading; 
 
    class SynchronizedDisposablePool where T : class, IDisposable
    { 
        List items;
        int maxCount;
        bool disposed;
 
        public SynchronizedDisposablePool(int maxCount)
        { 
            this.items = new List(); 
            this.maxCount = maxCount;
        } 

        object ThisLock
        {
            get { return this; } 
        }
 
        public void Dispose() 
        {
            T[] items; 
            lock (ThisLock)
            {
                if (!disposed)
                { 
                    disposed = true;
                    if (this.items.Count > 0) 
                    { 
                        items = new T[this.items.Count];
                        this.items.CopyTo(items, 0); 
                        this.items.Clear();
                    }
                    else
                    { 
                        items = null;
                    } 
                } 
                else
                { 
                    items = null;
                }
            }
            if (items != null) 
            {
                for (int i = 0; i < items.Length; i++) 
                { 
                    items[i].Dispose();
                } 
            }
        }

        public bool Return(T value) 
        {
            if (!disposed && this.items.Count < this.maxCount) 
            { 
                lock (ThisLock)
                { 
                    if (!disposed && this.items.Count < this.maxCount)
                    {
                        this.items.Add(value);
                        return true; 
                    }
                } 
            } 
            return false;
        } 

        public T Take()
        {
            if (!disposed && this.items.Count > 0) 
            {
                lock (ThisLock) 
                { 
                    if (!disposed && this.items.Count > 0)
                    { 
                        int index = this.items.Count - 1;
                        T item = this.items[index];
                        this.items.RemoveAt(index);
                        return item; 
                    }
                } 
            } 
            return null;
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK