GC.SuppressFinalize 方法
请求系统不要调用指定对象的终结器。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Sub SuppressFinalize ( _
obj As Object _
)
用法
Dim obj As Object
GC.SuppressFinalize(obj)
public static void SuppressFinalize (
Object obj
)
public:
static void SuppressFinalize (
Object^ obj
)
public static void SuppressFinalize (
Object obj
)
public static function SuppressFinalize (
obj : Object
)
参数
- obj
不得为其调用终结器的对象。
异常
异常类型 | 条件 |
---|---|
obj 为 空引用(在 Visual Basic 中为 Nothing)。 |
备注
该方法在对象头中设置一个位,系统在调用终结器时将检查这个位。obj 参数应为此方法的调用方。
实现 IDisposable 接口的对象可以从 IDisposable.Dispose 方法调用此方法,以防止垃圾回收器对不需要终止的对象调用 Object.Finalize。
示例
Imports System
Imports System.ComponentModel
' The following example demonstrates how to use the
' GC.SuppressFinalize method in a resource class to prevent
' the clean-up code for the object from being called twice.
Public Class DisposeExample
' A class that implements IDisposable.
' By implementing IDisposable, you are announcing that
' instances of this type allocate scarce resources.
Public Class MyResource
Implements IDisposable
' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private component As Component
' Track whether Dispose has been called.
Private disposed As Boolean = False
' The class constructor initializes the handle and component.
Public Sub New(ByVal handle As IntPtr)
Me.handle = handle
End Sub
' Implement IDisposable.
' Do not make this method virtual.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' This object will be cleaned up by the Dispose method.
' Therefore, you should call GC.SupressFinalize to
' take this object off the finalization queue
' and prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub
' 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.
Private Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not Me.disposed Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If disposing Then
' Dispose managed resources.
component.Dispose()
End If
' Call the appropriate methods to clean up
' unmanaged resources here.
' If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
End If
disposed = True
End Sub
' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel32")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
End Function
' This finalizer will run only if the Dispose method
' does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this class.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(False)
MyBase.Finalize()
End Sub
End Class
Public Shared Sub Main()
' Insert code here to create
' and use a MyResource object.
End Sub
End Class
using System;
using System.ComponentModel;
// The following example demonstrates how to use the
// GC.SuppressFinalize method in a resource class to prevent
// the clean-up code for the object from being called twice.
public class DisposeExample
{
// A class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public class MyResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private bool disposed = false;
// The class constructor.
public MyResource(IntPtr handle)
{
this.handle = handle;
}
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and 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.
private 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.
component.Dispose();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
}
disposed = true;
}
// Use interop to call the method necessary
// to clean up the unmanaged resource.
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
// 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.
~MyResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
}
public static void Main()
{
// Insert code here to create
// and use a MyResource object.
}
}
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
// The following example demonstrates how to use the
// GC::SuppressFinalize method to prevent finalization of an
// object that has been disposed. For C++, the call to
// GC::SuppressFinalize is not explicit; it is emitted by the
// compiler, in the Dispose() method. The comments in this code
// example explain where that call occurs and its part in the
// overall Dispose/Finalize pattern.
//
// The code example demonstrates how to create a class that
// implements the IDisposable interface and the IDisposable.Dispose
// method. By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources, which can be
// released by calling Dispose() when the programmer is finished
// with the object or which will be released when the finalizer
// is executed.
//
// Note that for This code example
//
public ref class MyResource: public IDisposable
{
private:
// Pointer to an external unmanaged resource.
IntPtr handle;
// A managed resource this class uses.
Component^ component;
// Track whether Dispose has been called.
bool disposed;
public:
// The class constructor.
MyResource( IntPtr handle, Component^ component )
{
this->handle = handle;
this->component = component;
disposed = false;
}
// In managed code, the destructor contains code to clean up managed
// resources. This method is called by Dispose(bool disposing),
// which the C++ compiler emits for you when you implement the
// destructor; it is only called if disposing == true -- that is,
// if the user has called Dispose(). (In C++, the user calls
// ~MyResource(), which the compiler emits as a call to Dispose().)
// The emitted Dispose() method calls GC::SuppressFinalize( this )
// for you, so there is no need to call it here.
~MyResource()
{
// Dispose of managed resources.
component->~Component();
disposed = true;
}
// Use interop to call the method necessary to clean up the
// unmanaged resource.
//
[System::Runtime::InteropServices::DllImport("Kernel32")]
static Boolean CloseHandle( IntPtr handle );
// Note: The Dispose(bool disposing) method emitted by the
// compiler executes in two distinct scenarios. If disposing ==
// true, the method has been called directly or indirectly by a
// user's code. Managed and unmanaged resources can be disposed,
// so both ~MyResource() and !MyResource() are called.
//
// 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, so
// only !MyResource() is called.
// This destructor runs when the Dispose method gets called
// explicitly, and also when (and if) the Finalizer is run.
!MyResource()
{
// Call the appropriate methods to clean up unmanaged
// resources here. If disposing is false when Dispose(bool,
// disposing) is called, only the following code is executed.
CloseHandle( handle );
handle = IntPtr::Zero;
}
};
void main()
{
// Insert code here to create and use the MyResource object.
MyResource^ mr = gcnew MyResource((IntPtr) 42, (Component^) gcnew Button());
mr->~MyResource();
}
import System.* ;
import System.ComponentModel.* ;
// The following example demonstrates how to use the
// GC.SuppressFinalize method in a resource class to prevent
// the clean-up code for the object from being called twice.
public class DisposeExample
{
// A class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public static class MyResource implements IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private boolean disposed = false;
// The class constructor.
public MyResource(IntPtr handle)
{
this.handle = handle;
} //MyResource
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
} //Dispose
// 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.
private void Dispose(boolean 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.
component.Dispose();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
}
disposed = true;
} //Dispose
// Use interop to call the method necessary to clean up the unmanaged
// resource.
/** @attribute System.Runtime.InteropServices.DllImport("Kernel32")
*/
private static native Boolean CloseHandle(IntPtr handle);
// Use VJ# 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.
public void finalize()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
try {
super.finalize();
}
catch (System.Exception e) {
}
} //finalize
} //MyResource
public static void main(String[] args)
{
// Insert code here to create
// and use a MyResource object.
} //main
} //DisposeExample
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0