本文转自:https://docs.microsoft.com/zh-cn/dotnet/api/system.gc.suppressfinalize?redirectedfrom=MSDN&view=netframework-4.8#System_GC_SuppressFinalize_System_Object_
请求公共语言运行时不要调用指定对象的终结器。
C#public static void SuppressFinalize (object obj);
参数
- obj
- Object
不得执行其终结器的对象。
例外
ArgumentNullExceptionobj
为 null
。
示例
下面的示例演示如何在资源类中使用 SuppressFinalize 方法,以防止调用冗余垃圾回收。 该示例使用dispose 模式来释放托管资源(即实现 IDisposable的对象)和非托管资源。
C#using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class ConsoleMonitor : IDisposable
{
const int STD_INPUT_HANDLE = -10;
const int STD_OUTPUT_HANDLE = -11;
const int STD_ERROR_HANDLE = -12;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteConsole(IntPtr hConsoleOutput, string lpBuffer,
uint nNumberOfCharsToWrite, out uint lpNumberOfCharsWritten,
IntPtr lpReserved);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
private bool disposed = false;
private IntPtr handle;
private Component component;
public ConsoleMonitor()
{
handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle == IntPtr.Zero)
throw new InvalidOperationException("A console handle is not available.");
component = new Component();
string output = "The ConsoleMonitor class constructor.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
}
// The destructor calls Object.Finalize.
~ConsoleMonitor()
{
if (handle != IntPtr.Zero) {
string output = "The ConsoleMonitor finalizer.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
}
else {
Console.Error.WriteLine("Object finalization.");
}
// Call Dispose with disposing = false.
Dispose(false);
}
public void Write()
{
string output = "The Write method.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
}
public void Dispose()
{
string output = "The Dispose method.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
string output = String.Format("The Dispose({0}) method.\n", disposing);
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
// Execute if resources have not already been disposed.
if (! disposed) {
// If the call is from Dispose, free managed resources.
if (disposing) {
Console.Error.WriteLine("Disposing of managed resources.");
if (component != null)
component.Dispose();
}
// Free unmanaged resources.
output = "Disposing of unmanaged resources.";
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
if (handle != IntPtr.Zero) {
if (! CloseHandle(handle))
Console.Error.WriteLine("Handle cannot be closed.");
}
}
disposed = true;
}
}
public class Example
{
public static void Main()
{
Console.WriteLine("ConsoleMonitor instance....");
ConsoleMonitor monitor = new ConsoleMonitor();
monitor.Write();
monitor.Dispose();
}
}
// If the monitor.Dispose method is not called, the example displays the following output:
// ConsoleMonitor instance....
// The ConsoleMonitor class constructor.
// The Write method.
// The ConsoleMonitor finalizer.
// The Dispose(False) method.
// Disposing of unmanaged resources.
//
// If the monitor.Dispose method is called, the example displays the following output:
// ConsoleMonitor instance....
// The ConsoleMonitor class constructor.
// The Write method.
// The Dispose method.
// The Dispose(True) method.
// Disposing of managed resources.
// Disposing of unmanaged resources.
注解
此方法设置 obj
的对象标头中的一个位,运行时在调用终结器时进行检查。 由 Object.Finalize 方法表示的终结器用于在对对象进行垃圾回收之前释放非托管资源。 如果 obj
没有终结器或 GC 已向终结器线程发出运行终结器的信号,则对 SuppressFinalize 方法的调用不起作用。
实现 IDisposable 接口的对象可以从对象的 IDisposable.Dispose 实现中调用此方法,以防止垃圾回收器在不需要的对象上调用 Object.Finalize。 通常,这样做是为了防止终结器释放已由 IDisposable.Dispose 实现释放的非托管资源。