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;}
- IDisposable in C#
- C# IDisposable
- C# IDisposable
- IDisposable, Finalizer, and SuppressFinalize in C# and C++/CLI
- C# IDisposable 接口
- C#中的IDisposable模式
- C#中IDisposable
- C#中的IDisposable接口
- IDisposable 在C#中的作用
- C#正确实现IDisposable接口
- IDisposable 在C#中的作用
- IDisposable 在C#中的作用
- IDisposable 在C#中的作用
- IDisposable 在C#中的作用
- IDisposable 在C#中的作用
- IDisposable 在C#中的作用
- C#中IDisposable接口实现
- C# ~ 由 IDisposable 到 GC
- 给Adobe Dreamweaver CS5安装jQuery语法提示插件
- JS的Document属性和方法 (抄)
- JS的Document属性和方法 (抄)
- java小程序
- Android JNI知识简介
- IDisposable in C#
- 深入Java关键字null
- Excel导入SQL方法
- Java中的Process和ProcessBuilder
- 2003系统启动项修改
- 大小端区别、Union和Struct的内存大小
- 利用微软的avi文件操作函数读取avi文件
- 有关Oracle 10g 更改字符集的问题
- Swing 记忆之给编辑框加上行号