IDisposable in C#

来源:互联网 发布:得利宝高收益网络理财 编辑:程序博客网 时间:2024/06/05 01:02

Dispose Pattern

The Dispose pattern defines the way you should implement dispose (and finalizer) functionality on all managed classes that maintain resources that the caller must be allowed to explicitly release. To implement the Dispose pattern, do the following:

  • Create a class that derives from IDisposable.
  • Add a private member variable to track whether IDisposable.Dispose has already been called. Clients should be allowed to call the method multiple times without generating an exception. If another method on the class is called after a call to Dispose, you should throw an ObjectDisposedException.
  • Implement a protected virtual void override of the Dispose method that accepts a single bool parameter. This method contains common cleanup code that is called either when the client explicitly calls IDisposable.Dispose or when the finalizer runs. The bool parameter is used to indicate whether the cleanup is being performed as a result of a client call to IDisposable.Dispose or as a result of finalization.
  • Implement the IDisposable.Dispose method that accepts no parameters. This method is called by clients to explicitly force the release of resources. Check whether Dispose has been called before; if it has not been called, call Dispose(true) and then prevent finalization by calling GC.SuppressFinalize(this). Finalization is no longer needed because the client has explicitly forced a release of resources.
  • Create a finalizer, by using destructor syntax. In the finalizer, call Dispose(false).

C# Example of Dispose

Your code should look like the following.

public sealed class MyClass: IDisposable{  // Variable to track if Dispose has been called  private bool disposed = false;  // Implement the IDisposable.Dispose() method  public void Dispose(){    // Check if Dispose has already been called     if (!disposed)    {      // Call the overridden Dispose method that contains common cleanup code      // Pass true to indicate that it is called from Dispose      Dispose(true);     // Prevent subsequent finalization of this object. This is not needed      // because managed and unmanaged resources have been explicitly released      GC.SuppressFinalize(this);    }  }  // Implement a finalizer by using destructor style syntax  ~MyClass() {    // Call the overridden Dispose method that contains common cleanup code    // Pass false to indicate the it is not called from Dispose    Dispose(false);  }  // Implement the override Dispose method that will contain common  // cleanup functionality  protected virtual void Dispose(bool disposing){   if(disposing){     // Dispose time code     . . .   }   // Finalize time code    . . .  }  …}

Passing true to the protected Dispose method ensures that dispose specific code is called. Passing false skips the Dispose specific code. The Dispose(bool) method can be called directly by your class or indirectly by the client.

If you reference any static variables or methods in your finalize-time Dispose code, make sure you check the Environment.HasShutdownStarted property. If your object is thread safe, be sure to take whatever locks are necessary for cleanup.

Use the HasShutdownStarted property in an object's Dispose method to determine whether the CLR is shutting down or the application domain is unloading. If that is the case, you cannot reliably access any object that has a finalization method and is referenced by a static field.

protected virtual void Dispose(bool disposing){  if(disposing){    // dispose-time code  . . .  }  // finalize-time code  CloseHandle();  if(!Environment.HasShutDownStarted)   { //Debug.Write or Trace.Write – static methods    Debug.WriteLine("Finalizer Called");  }  disposed = true;}
原创粉丝点击