实现 Dispose 方法

实现 Dispose 方法

MSDNios

类型的 Dispose 方法应释放它拥有的全部资源。它还应该经过调用其父类型的 Dispose 方法释放其基类型拥有的全部资源。该父类型的 Dispose 方法应该释放它拥有的全部资源并一样也调用其父类型的 Dispose 方法,从而在整个基类型层次结构中传播此模式。若要确保始终正确地清理资源,Dispose 方法应该能够被屡次调用而不引起任何异常。程序员

Note要点

C++ 程序员不该该使用本主题。而应参见 Destructors and Finalizers in Visual C++。在 .NET Framework 2.0 版中,C++ 编译器为实现资源的肯定性处置提供支持,而且不容许直接实现Dispose 方法。设计模式

Dispose 方法应该为它处置的对象调用 GC.SuppressFinalize 方法。若是对象当前在终止队列中,GC.SuppressFinalize 防止其 Finalize 方法被调用。请记住,执行 Finalize 方法会大大减损性能。若是您的 Dispose 方法已经完成了清理对象的工做,那么垃圾回收器就没必要再调用对象的 Finalize 方法。安全

Note注意

为 System.GC.KeepAlive(System.Object) 方法提供的代码示例演示了强行垃圾回收如何在回收对象的成员仍在执行时引发终结器运行。在较长的 Dispose 方法末尾最好调用 KeepAlive 方法。app

下面的代码示例旨在阐释用于为封装了非托管资源的类实现 Dispose 方法的建议设计模式。整个 .NET Framework 中都实现了此模式。异步

资源类一般是从复杂的本机类或 API 派生的,并且必须进行相应的自定义。使用这一代码模式做为建立资源类的一个起始点,并根据封装的资源提供必要的自定义。不能编译该示例,也不能将其直接用于应用程序。ide

在此示例中,基类 BaseResource 实现可由该类的用户调用的公共 Dispose 方法。而该方法又调用 virtual Dispose(bool disposing) 方法(Visual Basic 中为 virtual Dispose(disposing As Boolean))。根据调用方的标识传递 true 或 false。以虚 Dispose 方法为对象执行适当的清理代码。函数

Dispose(bool disposing) 以两种大相径庭的方案执行。若是 disposing 等于 true,则该方法已由用户的代码直接调用或间接调用,而且可释放托管资源和非托管资源。若是 disposing 等于false,则该方法已由运行库从终结器内部调用,而且只能释放非托管资源。由于终结器不会以任意特定的顺序执行,因此当对象正在执行其终止代码时,不该引用其余对象。若是正在执行的终结器引用了另外一个已经终止的对象,则该正在执行的终结器将失败。性能

基类提供的 Finalize 方法或析构函数在未能调用 Dispose 的状况下充当防御措施。Finalize 方法调用带有参数的 Dispose 方法,同时传递 false。不该在 Finalize 方法内从新建立 Dispose 清理代码。调用 Dispose(false) 能够优化代码的可读性和可维护性。优化

类 MyResourceWrapper 阐释如何使用 Dispose 从实现资源管理的类派生。MyResourceWrapper 重写 virtual Dispose(bool disposing) 方法并为其建立的托管和非托管资源提供清理代码。MyResourceWrapper 还对其基类 BaseResource 调用 Dispose 以确保其基类可以适当地进行清理。请注意,派生类 MyResourceWrapper 没有不带参数的 Finalize 方法或 Dispose 方法,由于这两个方法从基类 BaseResource 继承这些参数。

Note注意

此示例中的 protected Dispose(bool disposing) 方法不强制线程安全,由于没法从用户线程和终结器线程同时调用该方法。另外,使用 BaseResource 的客户端应用程序应从不容许多个用户线程同时调用 protected Dispose(bool disposing) 方法。应用程序或类库的设计原则为:应用程序或类库应只容许一个线程拥有资源的生存期,而且应在再也不须要资源时调用 Dispose。根据资源的不一样,在处置资源时进行异步线程访问可能会带来安全风险。开发人员应仔细检查本身的代码,以肯定最佳的方法来强制线程安全。

// Design pattern for the base class.
// By implementing IDisposable, you are announcing that instances
// of this type allocate scarce resources.
public class BaseResource: IDisposable
{
   // Pointer to an external unmanaged resource.
   private IntPtr handle;
   // Other managed resource this class uses.
   private Component Components;
   // Track whether Dispose has been called.
   private bool disposed = false;

   // Constructor for the BaseResource object.
   public BaseResource()
   {
      // Insert appropriate constructor code here.
   }

   // Implement IDisposable.
   // Do not make this method virtual.
   // A derived class should not be able to override this method.
   public void Dispose()
   {
      Dispose(true);
      // Take yourself off the Finalization queue 
      // to prevent finalization code for this object
      // from executing a second time.
      GC.SuppressFinalize(this);
   }

   // Dispose(bool disposing) executes in two distinct scenarios.
   // If disposing equals true, the method has been called directly
   // or indirectly by a user's code. Managed and unmanaged resources
   // can be disposed.
   // If disposing equals false, the method has been called by the 
   // runtime from inside the finalizer and you should not reference 
   // other objects. Only unmanaged resources can be disposed.
   protected virtual void Dispose(bool disposing)
   {
      // Check to see if Dispose has already been called.
      if(!this.disposed)
      {
         // If disposing equals true, dispose all managed 
         // and unmanaged resources.
         if(disposing)
         {
            // Dispose managed resources.
            Components.Dispose();
         }
         // Release unmanaged resources. If disposing is false, 
         // only the following code is executed.
         CloseHandle(handle);
         handle = IntPtr.Zero;
         // Note that this is not thread safe.
         // Another thread could start disposing the object
         // after the managed resources are disposed,
         // but before the disposed flag is set to true.
         // If thread safety is necessary, it must be
         // implemented by the client.

      }
      disposed = true;         
   }

   // Use C# destructor syntax for finalization code.
   // This destructor will run only if the Dispose method 
   // does not get called.
   // It gives your base class the opportunity to finalize.
   // Do not provide destructors in types derived from this class.
   ~BaseResource()      
   {
      // Do not re-create Dispose clean-up code here.
      // Calling Dispose(false) is optimal in terms of
      // readability and maintainability.
      Dispose(false);
   }

   // Allow your Dispose method to be called multiple times,
   // but throw an exception if the object has been disposed.
   // Whenever you do something with this class, 
   // check to see if it has been disposed.
   public void DoSomething()
   {
      if(this.disposed)
      {
         throw new ObjectDisposedException();
      }
   }
}

// Design pattern for a derived class.
// Note that this derived class inherently implements the 
// IDisposable interface because it is implemented in the base class.
public class MyResourceWrapper: BaseResource
{
   // A managed resource that you add in this derived class.
   private ManagedResource addedManaged;
   // A native unmanaged resource that you add in this derived class.
   private NativeResource addedNative;
   private bool disposed = false;

  // Constructor for this object.
   public MyResourceWrapper()
   {
      // Insert appropriate constructor code here.
   }

   protected override void Dispose(bool disposing)
   {
      if(!this.disposed)
      {
         try
         {
            if(disposing)
            {
               // Release the managed resources you added in
               // this derived class here.
               addedManaged.Dispose();         
            }
            // Release the native unmanaged resources you added
            // in this derived class here.
            CloseHandle(addedNative);
            this.disposed = true;
         }
         finally
         {
            // Call Dispose on your base class.
            base.Dispose(disposing);
         }
      }
   }
}

// This derived class does not have a Finalize method
// or a Dispose method without parameters because it inherits 
// them from the base class.

实现 Close 方法

对于类型来讲,若调用 Close 方法比调用 Dispose 方法更容易,则能够向基类型添加一个公共 Close 方法。Close 方法又会调用没有参数的 Dispose 方法,该方法能够执行正确的清理操做。下面的代码示例阐释了 Close 方法。

// Do not make this method virtual.
// A derived class should not be allowed
// to override this method.
public void Close()
{
   // Calls the Dispose method without parameters.
   Dispose();
}

请参见

相关文章
相关标签/搜索