CriticalFinalizerObject 類別


確保衍生類別中所有的最終處理程式碼都標記為關鍵。Ensures that all finalization code in derived classes is marked as critical.

public ref class CriticalFinalizerObject abstract
public abstract class CriticalFinalizerObject
public abstract class CriticalFinalizerObject
type CriticalFinalizerObject = class
type CriticalFinalizerObject = class
Public MustInherit Class CriticalFinalizerObject


下列程式碼範例示範 SafeFileHandle 如何使用類別來提供標準輸入和輸出資料流程的重要完成項。The following code example shows the use of the SafeFileHandle class to provide critical finalization for the standard input and output streams. SafeFileHandle衍生自 SafeHandle 類別的會傳遞至函式中的檔案資料流程 FileStreamThe SafeFileHandle, derived from the SafeHandle class, is passed to the file stream in the FileStream constructor.

using System;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32.SafeHandles;

namespace CriticalFinalizer
    class Program
        const int STD_INPUT_HANDLE   = -10;
        const int STD_OUTPUT_HANDLE = -11;
        const int STD_ERROR_HANDLE  =  -12;
        [DllImport("Kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern IntPtr GetStdHandle(int type);

        static void Main(string[] args)
            FileStream fsIn = null;
            FileStream fsOut = null;
                SafeFileHandle sfhIn = new SafeFileHandle(GetStdHandle(STD_INPUT_HANDLE), false);
                fsIn = new FileStream(sfhIn, FileAccess.Read);
                byte[] input = new byte[] {0};
                SafeFileHandle sfhOut = new SafeFileHandle(GetStdHandle(STD_OUTPUT_HANDLE), false);
                fsOut = new FileStream(sfhOut, FileAccess.Write);
                SafeFileHandle sf = fsOut.SafeFileHandle;
                if (fsIn != null)
                    fsIn = null;
                if (fsOut != null)
                    fsOut = null;
Imports System.Runtime.InteropServices
Imports System.IO
Imports Microsoft.Win32.SafeHandles

Public Module Example
   Const STD_INPUT_HANDLE As Integer  = -10
   Const STD_OUTPUT_HANDLE As Integer = -11
   Const STD_ERROR_HANDLE As Integer  = -12

   Public Declare Auto Function GetStdHandle Lib "Kernel32" (type As Integer) As IntPtr

   Public Sub Main()
      Dim fsIn As FileStream = Nothing
      Dim fsOut As FileStream = Nothing

         Dim sfhIn As New SafeFileHandle(GetStdHandle(STD_INPUT_HANDLE), False)
         fsIn = new FileStream(sfhIn, FileAccess.Read)
         Dim input() As Byte = { 0 }
         fsIn.Read(input, 0, 1)
         Dim sfhOut As New SafeFileHandle(GetStdHandle(STD_OUTPUT_HANDLE), False)
         fsOut = New FileStream(sfhOut, FileAccess.Write)
         fsOut.Write(input, 0, 1)
         Dim sf As SafeFileHandle = fsOut.SafeFileHandle
         If fsIn IsNot Nothing Then
            fsIn = Nothing
         End If
         If fsOut IsNot Nothing Then 
            fsOut = Nothing
         End If
      End Try
   End Sub
End Module


衍生自類別的類別 CriticalFinalizerObject 會隱含地被視為受限制的執列區域 (CER) 。Classes deriving from the CriticalFinalizerObject class are implicitly treated as a constrained execution region (CER). 這需要完成項中的程式碼,才能以強式可靠性合約呼叫程式碼。This requires code in the finalizer to only call code with a strong reliability contract. 如需 Cer 的詳細資訊,請參閱 System.Runtime.ConstrainedExecution 命名空間。For more information about CERs, see the System.Runtime.ConstrainedExecution namespace.

在衍生自類別的類別中 CriticalFinalizerObject ,通用語言執行平臺 (CLR) 保證所有關鍵的完成程式碼都有機會執行,但前提是如果在 CLR 強制卸載應用程式域或中止執行緒的情況下,完成項會遵循 CER 的規則。In classes derived from the CriticalFinalizerObject class, the common language runtime (CLR) guarantees that all critical finalization code will be given the opportunity to execute, provided the finalizer follows the rules for a CER, even in situations where the CLR forcibly unloads an application domain or aborts a thread. 如果完成項違反 CER 的規則,則可能無法順利執行。If a finalizer violates the rules for a CER, it might not successfully execute. 此外,CLR 會在一般和關鍵完成項之間建立弱式排序:對於垃圾收集同時回收的物件,所有非關鍵的完成項都會在任何重要的完成項之前呼叫。In addition, the CLR establishes a weak ordering among normal and critical finalizers: for objects reclaimed by garbage collection at the same time, all the noncritical finalizers are called before any of the critical finalizers. 例如,在 FileStream 衍生自的類別中保存資料的類別, SafeHandle CriticalFinalizerObject 可以執行標準完成項來清除現有的緩衝資料。For example, a class such as FileStream, which holds data in the SafeHandle class that is derived from CriticalFinalizerObject, can run a standard finalizer to flush out existing buffered data.

在大多數情況下,您不需要撰寫衍生自類別的類別 CriticalFinalizerObjectIn most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. .NET Framework 類別庫提供兩個類別, SafeHandle 以及 CriticalHandle 提供處理資源的重要最終處理功能。The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. 此外,.NET Framework 提供一組衍生自類別的預先撰寫類別 SafeHandle ,而此集合位於 Microsoft.Win32.SafeHandles 命名空間中。Furthermore, the .NET Framework provides a set of prewritten classes derived from the SafeHandle class, and this set is located in the Microsoft.Win32.SafeHandles namespace. 這些類別是設計用來提供支援檔案和作業系統控制碼的一般功能。These classes are designed to provide common functionality for supporting file and operating system handles.



初始化 CriticalFinalizerObject 類別的新執行個體。Initializes a new instance of the CriticalFinalizerObject class.



判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)

釋放 CriticalFinalizerObject 類別使用的所有資源。Releases all the resources used by the CriticalFinalizerObject class.


做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)