andrew4582

ResourcePool

Feb 18th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. internal class ResourcePool : IDisposable
  2. {
  3.     private TimerCallback _callback;
  4.     private bool _disposed;
  5.     private int _iDisposable;
  6.     private TimeSpan _interval;
  7.     private int _max;
  8.     private ArrayList _resources;
  9.     private Timer _timer;
  10.  
  11.     internal ResourcePool(TimeSpan interval, int max)
  12.     {
  13.         this._interval = interval;
  14.         this._resources = new ArrayList(4);
  15.         this._max = max;
  16.         this._callback = new TimerCallback(this.TimerProc);
  17.     }
  18.  
  19.     [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
  20.     public void Dispose()
  21.     {
  22.         this.Dispose(true);
  23.     }
  24.  
  25.     private void Dispose(bool disposing)
  26.     {
  27.         lock (this)
  28.         {
  29.             if (!this._disposed)
  30.             {
  31.                 if (this._resources != null)
  32.                 {
  33.                     foreach (IDisposable disposable in this._resources)
  34.                     {
  35.                         disposable.Dispose();
  36.                     }
  37.                     this._resources.Clear();
  38.                 }
  39.                 if (this._timer != null)
  40.                 {
  41.                     this._timer.Dispose();
  42.                 }
  43.                 this._disposed = true;
  44.                 if (disposing)
  45.                 {
  46.                     GC.SuppressFinalize(this);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     ~ResourcePool()
  53.     {
  54.         this.Dispose(false);
  55.     }
  56.  
  57.     internal object RetrieveResource()
  58.     {
  59.         object obj2 = null;
  60.         if (this._resources.Count != 0)
  61.         {
  62.             lock (this)
  63.             {
  64.                 if (this._disposed)
  65.                 {
  66.                     return obj2;
  67.                 }
  68.                 if (this._resources.Count == 0)
  69.                 {
  70.                     return null;
  71.                 }
  72.                 obj2 = this._resources[this._resources.Count - 1];
  73.                 this._resources.RemoveAt(this._resources.Count - 1);
  74.                 if (this._resources.Count < this._iDisposable)
  75.                 {
  76.                     this._iDisposable = this._resources.Count;
  77.                 }
  78.             }
  79.         }
  80.         return obj2;
  81.     }
  82.  
  83.     internal void StoreResource(IDisposable o)
  84.     {
  85.         lock (this)
  86.         {
  87.             if (!this._disposed && (this._resources.Count < this._max))
  88.             {
  89.                 this._resources.Add(o);
  90.                 o = null;
  91.                 if (this._timer == null)
  92.                 {
  93.                     this._timer = new Timer(this._callback, null, this._interval, this._interval);
  94.                 }
  95.             }
  96.         }
  97.         if (o != null)
  98.         {
  99.             o.Dispose();
  100.         }
  101.     }
  102.  
  103.     private void TimerProc(object userData)
  104.     {
  105.         IDisposable[] array = null;
  106.         lock (this)
  107.         {
  108.             if (!this._disposed)
  109.             {
  110.                 if (this._resources.Count == 0)
  111.                 {
  112.                     if (this._timer != null)
  113.                     {
  114.                         this._timer.Dispose();
  115.                         this._timer = null;
  116.                     }
  117.                     return;
  118.                 }
  119.                 array = new IDisposable[this._iDisposable];
  120.                 this._resources.CopyTo(0, array, 0, this._iDisposable);
  121.                 this._resources.RemoveRange(0, this._iDisposable);
  122.                 this._iDisposable = this._resources.Count;
  123.             }
  124.         }
  125.         if (array != null)
  126.         {
  127.             for (int i = 0; i < array.Length; i++)
  128.             {
  129.                 try
  130.                 {
  131.                     array[i].Dispose();
  132.                 }
  133.                 catch
  134.                 {
  135.                 }
  136.             }
  137.         }
  138.     }
  139. }
Add Comment
Please, Sign In to add comment