C# 托管资源与非托管资源(参考七)

本文转自: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

不得执行其终结器的对象。

例外

ArgumentNullException

obj 为 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 实现释放的非托管资源。

适用于

.NET

5 Preview 1

.NET Core

3.1 3.0 2.2 2.1 2.0 1.1 1.0

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1

.NET Standard

2.1 2.0 1.6 1.5 1.4 1.3 1.2 1.1 1.0

UWP

10.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0
上一篇:用51单片机,keil编程,proteus仿真,实现2位数码管00-99自动计数


下一篇:C# 检查硬盘分区是ssd还是hdd