GC.ReRegisterForFinalize 메서드
SuppressFinalize가 이전에 호출된 지정된 개체에 대해 시스템에서 종료자를 호출하도록 요청합니다.
네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
Public Shared Sub ReRegisterForFinalize ( _
obj As Object _
)
‘사용 방법
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
)
매개 변수
- obj
종료자가 호출되어야 하는 개체입니다.
예외
예외 형식 | 조건 |
---|---|
obj가 Null 참조(Visual Basic의 경우 Nothing)인 경우 |
설명
ReRegisterForFinalize 메서드는 obj를 가비지 수집기가 개체를 해제하기 전에 종료를 필요로 하는 개체의 목록에 추가합니다. obj 매개 변수는 이 메서드의 호출자이어야 합니다.
ReRegisterForFinalize 메서드를 호출한다고 해도 가비지 수집기가 개체의 종료자를 호출할 것을 보장하지는 않습니다.
기본적으로, 종료자를 구현하는 모든 개체는 종료를 필요로 하는 개체의 목록에 추가됩니다. 하지만 개체가 이미 종결되었거나 SuppressFinalize 메서드를 호출하여 종료를 불가능하도록 했을 수도 있습니다.
종료자는 이 메서드를 사용하여 자신 또는 자신이 참조하는 개체를 재활성화할 수 있습니다.
예제
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
플랫폼
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에서 지원