Advertisement
jamzc92

DisposableAction.cs

Jun 12th, 2015
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. namespace Croft.MicrosoftBand.Common
  2. {
  3.     using System;
  4.  
  5.     /// <summary>
  6.     /// The disposable action.
  7.     /// </summary>
  8.     public class DisposableAction : IDisposable
  9.     {
  10.         private Action _dispose;
  11.  
  12.         /// <summary>
  13.         /// Initializes a new instance of the <see cref="DisposableAction"/> class.
  14.         /// </summary>
  15.         /// <param name="dispose">
  16.         /// The dispose.
  17.         /// </param>
  18.         public DisposableAction(Action dispose)
  19.         {
  20.             if (dispose == null) throw new ArgumentNullException("dispose");
  21.  
  22.             this._dispose = dispose;
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Initializes a new instance of the <see cref="DisposableAction"/> class.
  27.         /// </summary>
  28.         /// <param name="construct">
  29.         /// The construct.
  30.         /// </param>
  31.         /// <param name="dispose">
  32.         /// The dispose.
  33.         /// </param>
  34.         public DisposableAction(Action construct, Action dispose)
  35.         {
  36.             if (construct == null) throw new ArgumentNullException("construct");
  37.             if (dispose == null) throw new ArgumentNullException("dispose");
  38.  
  39.             construct();
  40.  
  41.             this._dispose = dispose;
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  46.         /// </summary>
  47.         /// <filterpriority>2</filterpriority>
  48.         public void Dispose()
  49.         {
  50.             this.Dispose(true);
  51.             GC.SuppressFinalize(this);
  52.         }
  53.  
  54.         /// <summary>
  55.         /// The dispose.
  56.         /// </summary>
  57.         /// <param name="disposing">
  58.         /// The disposing.
  59.         /// </param>
  60.         protected virtual void Dispose(bool disposing)
  61.         {
  62.             if (disposing)
  63.             {
  64.                 if (this._dispose == null)
  65.                 {
  66.                     return;
  67.                 }
  68.  
  69.                 try
  70.                 {
  71.                     this._dispose();
  72.                 }
  73.                 catch (Exception ex)
  74.                 {
  75.                     // ToDo: Log error?
  76.                 }
  77.  
  78.                 this._dispose = null;
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement