Freigeben über


GC.ReRegisterForFinalize-Methode

Fordert beim System den Aufruf des Finalizers für das angegebene Objekt an, für das zuvor SuppressFinalize aufgerufen wurde.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Sub ReRegisterForFinalize ( _
    obj As Object _
)
'Usage
Dim obj As Object

GC.ReRegisterForFinalize(obj)
public static void ReRegisterForFinalize (
    Object obj
)
public:
static void ReRegisterForFinalize (
    Object^ obj
)
public static void ReRegisterForFinalize (
    Object obj
)
public static function ReRegisterForFinalize (
    obj : Object
)

Parameter

  • obj
    Das Objekt, für das ein Finalizer aufgerufen werden muss.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

obj ist NULL (Nothing in Visual Basic).

Hinweise

Die ReRegisterForFinalize-Methode fügt obj der Liste der Objekte hinzu, für die vor der Freigabe durch den Garbage Collector eine Finalisierung angefordert wird. Der obj-Parameter muss der Aufrufer dieser Methode sein.

Der Aufruf der ReRegisterForFinalize-Methode garantiert nicht, dass der Garbage Collector den Finalizer des Objekts aufruft.

In der Standardeinstellung werden alle Objekte, die Finalizer implementieren, in die Liste der Objekte aufgenommen, die finalisiert werden müssen. Jedoch kann ein Objekt bereits finalisiert worden sein, oder die Finalisierung kann durch einen Aufruf der SuppressFinalize-Methode deaktiviert worden sein.

Ein Finalizer kann diese Methode aufrufen, um das eigene Objekt oder ein Objekt, auf das er verweist, wieder zugänglich zu machen.

Beispiel

Imports System

Namespace ReRegisterForFinalizeExample
    Class MyMainClass
        Shared Sub Main()
            'Create a MyFinalizeObject.
            Dim mfo As New MyFinalizeObject()

            'Release the reference to mfo.
            mfo = Nothing

            'Force a garbage collection.
            GC.Collect()

            'At this point mfo will have gone through the first Finalize.
            'There should now be a reference to mfo in the static
            'MyFinalizeObject.currentInstance field.  Setting this value
            'to null and forcing another garbage collection will now
            'cause the object to Finalize permanently.
            MyFinalizeObject.currentInstance = Nothing
            GC.Collect()
        End Sub
    End Class

    Class MyFinalizeObject
        Public Shared currentInstance As MyFinalizeObject = Nothing
        Private hasFinalized As Boolean = False

        Protected Overrides Sub Finalize()
            If hasFinalized = False Then
                Console.WriteLine("First finalization")

                'Put this object back into a root by creating
                'a reference to it.
                MyFinalizeObject.currentInstance = Me

                'Indicate that this instance has finalized once.
                hasFinalized = True

                'Place a reference to this object back in the
                'finalization queue.
                GC.ReRegisterForFinalize(Me)
            Else
                Console.WriteLine("Second finalization")
            End If
            MyBase.Finalize()
        End Sub
    End Class
End Namespace
using System;

namespace ReRegisterForFinalizeExample
{
    class MyMainClass
    {
        static void Main()
        {
            // Create a MyFinalizeObject.
            MyFinalizeObject mfo = new MyFinalizeObject();

            // Release the reference to mfo.
            mfo = null;

            // Force a garbage collection.
            GC.Collect();

            // At this point mfo will have gone through the first Finalize.
            // There should now be a reference to mfo in the static
            // MyFinalizeObject.currentInstance field.  Setting this value
            // to null and forcing another garbage collection will now
            // cause the object to Finalize permanently.
            MyFinalizeObject.currentInstance = null;
            GC.Collect();
        }
    }

    class MyFinalizeObject
    {
        public static MyFinalizeObject currentInstance = null;
        private bool hasFinalized = false;

        ~MyFinalizeObject()
        {
            if(hasFinalized == false)
            {
                Console.WriteLine("First finalization");
            
                // Put this object back into a root by creating
                // a reference to it.
                MyFinalizeObject.currentInstance = this;
            
                // Indicate that this instance has finalized once.
                hasFinalized = true;

                // Place a reference to this object back in the
                // finalization queue.
                GC.ReRegisterForFinalize(this);
            }
            else
            {
                Console.WriteLine("Second finalization");
            }
        }
    }
}
using namespace System;
ref class MyFinalizeObject
{
public:
   static MyFinalizeObject^ currentInstance = nullptr;

private:
   bool hasFinalized;

public:
   MyFinalizeObject()
   {
      hasFinalized = false;
   }

   ~MyFinalizeObject()
   {
      if ( hasFinalized == false )
      {
         Console::WriteLine( "First finalization" );
         
         // Put this object back into a root by creating
         // a reference to it.
         MyFinalizeObject::currentInstance = this;
         
         // Indicate that this instance has finalized once.
         hasFinalized = true;
         
         // Place a reference to this object back in the
         // finalization queue.
         GC::ReRegisterForFinalize( this );
      }
      else
      {
         Console::WriteLine( "Second finalization" );
      }
   }

};

int main()
{
   
   // Create a MyFinalizeObject.
   MyFinalizeObject^ mfo = gcnew MyFinalizeObject;
   
   // Release the reference to mfo.
   mfo = nullptr;
   
   // Force a garbage collection.
   GC::Collect();
   
   // At this point mfo will have gone through the first Finalize.
   // There should now be a reference to mfo in the static
   // MyFinalizeObject::currentInstance field.  Setting this value
   // to 0 and forcing another garbage collection will now
   // cause the object to Finalize permanently.
   MyFinalizeObject::currentInstance = nullptr;
   GC::Collect();
}
package ReRegisterForFinalizeExample;

import System.* ;

class MyMainClass
{
    public static void main(String[] args)
    {
        // Create a MyFinalizeObject.
        MyFinalizeObject mfo = new MyFinalizeObject();

        // Release the reference to mfo.
        mfo = null;

        // Force a garbage collection.
        GC.Collect();

        // At this point mfo will have gone through the first Finalize.
        // There should now be a reference to mfo in the static
        // MyFinalizeObject.currentInstance field.  Setting this value
        // to null and forcing another garbage collection will now
        // cause the object to Finalize permanently.
        MyFinalizeObject.currentInstance = null;
        GC.Collect();
    } //main
} //MyMainClass

class MyFinalizeObject
{
    public static MyFinalizeObject currentInstance = null;
    private boolean hasFinalized = false;

    public void finalize()
    {
        if (hasFinalized == false) {
            Console.WriteLine("First finalization");

            // Put this object back into a root by creating
            // a reference to it.
            MyFinalizeObject.currentInstance = this;

            // Indicate that this instance has finalized once.
            hasFinalized = true;

            // Place a reference to this object back in the
            // finalization queue.
            GC.ReRegisterForFinalize(this);
        }
        else {
            Console.WriteLine("Second finalization");
        }

        try {
            super.finalize();
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message());
        }
    } //finalize
} //MyFinalizeObject

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

GC-Klasse
GC-Member
System-Namespace
SuppressFinalize