GC.WaitForPendingFinalizers GC.WaitForPendingFinalizers GC.WaitForPendingFinalizers GC.WaitForPendingFinalizers Method

定义

挂起当前线程,直到处理终结器队列的线程清空该队列为止。Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue.

public:
 static void WaitForPendingFinalizers();
public static void WaitForPendingFinalizers ();
static member WaitForPendingFinalizers : unit -> unit
Public Shared Sub WaitForPendingFinalizers ()

示例

下面的示例演示如何使用 WaitForPendingFinalizers 方法挂起当前线程,直到完成所有收集的对象的终止为止。The following example demonstrates how to use the WaitForPendingFinalizers method to suspend the current thread until finalization of all the collected objects is complete.

using namespace System;
ref class MyFinalizeObject
{
private:

   // Make this number very large to cause the finalizer to
   // do more work.
   literal int maxIterations = 10000;
   ~MyFinalizeObject()
   {
      Console::WriteLine( "Finalizing a MyFinalizeObject" );
      
      // Do some work.
      for ( int i = 0; i < maxIterations; i++ )
      {
         
         // This method performs no operation on i, but prevents
         // the JIT compiler from optimizing away the code inside
         // the loop.
         GC::KeepAlive( i );

      }
   }

};


// You can increase this number to fill up more memory.
const int numMfos = 1000;

// You can increase this number to cause more
// post-finalization work to be done.
const int maxIterations = 100;
int main()
{
   MyFinalizeObject^ mfo = nullptr;
   
   // Create and release a large number of objects
   // that require finalization.
   for ( int j = 0; j < numMfos; j++ )
   {
      mfo = gcnew MyFinalizeObject;

   }
   
   //Release the last object created in the loop.
   mfo = nullptr;
   
   //Force garbage collection.
   GC::Collect();
   
   // Wait for all finalizers to complete before continuing.
   // Without this call to GC::WaitForPendingFinalizers,
   // the worker loop below might execute at the same time
   // as the finalizers.
   // With this call, the worker loop executes only after
   // all finalizers have been called.
   GC::WaitForPendingFinalizers();
   
   // Worker loop to perform post-finalization code.
   for ( int i = 0; i < maxIterations; i++ )
   {
      Console::WriteLine( "Doing some post-finalize work" );

   }
}

using System;

namespace WaitForPendingFinalizersExample
{
   class MyWaitForPendingFinalizersClass
   {
    // You can increase this number to fill up more memory.
    const int numMfos = 1000;
    // You can increase this number to cause more
    // post-finalization work to be done.
    const int maxIterations = 100;

    static void Main(string[] args)
    {
       MyFinalizeObject mfo = null;
         
       // Create and release a large number of objects
       // that require finalization.
       for(int j = 0; j < numMfos; j++)
       {
          mfo = new MyFinalizeObject();
       }
         
       //Release the last object created in the loop.
       mfo = null;

       //Force garbage collection.
       GC.Collect();
         
       // Wait for all finalizers to complete before continuing.
       // Without this call to GC.WaitForPendingFinalizers, 
       // the worker loop below might execute at the same time 
       // as the finalizers.
       // With this call, the worker loop executes only after
       // all finalizers have been called.
       GC.WaitForPendingFinalizers();

       // Worker loop to perform post-finalization code.
       for(int i = 0; i < maxIterations; i++)
       {
          Console.WriteLine("Doing some post-finalize work");
       }
    }
   }

   class MyFinalizeObject
   {
    // Make this number very large to cause the finalizer to
    // do more work.
    private const int maxIterations = 10000;
      
    ~MyFinalizeObject()
    {
       Console.WriteLine("Finalizing a MyFinalizeObject");
            
       // Do some work.
       for(int i = 0; i < maxIterations; i++)
       {
          // This method performs no operation on i, but prevents 
          // the JIT compiler from optimizing away the code inside 
          // the loop.
          GC.KeepAlive(i);
       }
        }
    }
}
Namespace WaitForPendingFinalizersExample
   Class MyWaitForPendingFinalizersClass
  
    ' You can increase this number to fill up more memory.
      Private Const numMfos As Integer = 1000
      ' You can increase this number to cause more
      ' post-finalization work to be done.
      Private Const maxIterations As Integer = 100
     
      Overloads Shared Sub Main()
         Dim mfo As MyFinalizeObject = Nothing
      
         ' Create and release a large number of objects
         ' that require finalization.
         Dim j As Integer
         For j = 0 To numMfos - 1
            mfo = New MyFinalizeObject()
         Next j
      
         'Release the last object created in the loop.
         mfo = Nothing
      
         'Force garbage collection.
         GC.Collect()
      
         ' Wait for all finalizers to complete before continuing.
         ' Without this call to GC.WaitForPendingFinalizers, 
         ' the worker loop below might execute at the same time 
         ' as the finalizers.
         ' With this call, the worker loop executes only after
         ' all finalizers have been called.
         GC.WaitForPendingFinalizers()
      
         ' Worker loop to perform post-finalization code.
         Dim i As Integer
         For i = 0 To maxIterations - 1
            Console.WriteLine("Doing some post-finalize work")
         Next i
      End Sub
   End Class


   Class MyFinalizeObject
      ' Make this number very large to cause the finalizer to
      ' do more work.
      Private maxIterations As Integer = 10000
      
      Protected Overrides Sub Finalize()
         Console.WriteLine("Finalizing a MyFinalizeObject")
      
         ' Do some work.
         Dim i As Integer
         For i = 0 To maxIterations - 1
            ' This method performs no operation on i, but prevents 
            ' the JIT compiler from optimizing away the code inside 
            ' the loop.
            GC.KeepAlive(i)
         Next i
         MyBase.Finalize()
      End Sub
   End Class
End Namespace

注解

当垃圾回收器发现可回收的对象时,它会检查每个对象以确定对象的终止要求。When the garbage collector finds objects that can be reclaimed, it checks each object to determine the object's finalization requirements. 如果对象实现终结器,且尚未通过调用 @no__t 禁用完成,则该对象将被置于标记为 "已准备好终止" 的对象列表中。If an object implements a finalizer and has not disabled finalization by calling SuppressFinalize, the object is placed in a list of objects that are marked as ready for finalization. 垃圾回收器为此列表中的对象调用 @no__t 的方法,并从列表中删除这些项。The garbage collector calls the Finalize methods for the objects in this list and removes the entries from the list. 此方法会一直阻止,直到所有终结器都已运行完毕。This method blocks until all finalizers have run to completion.

未指定运行终结器的线程,因此无法保证此方法将终止。The thread on which finalizers are run is unspecified, so there is no guarantee that this method will terminate. 但是,当 WaitForPendingFinalizers 方法正在进行时,此线程可能会被另一个线程中断。However, this thread can be interrupted by another thread while the WaitForPendingFinalizers method is in progress. 例如,你可以启动等待一段时间的另一个线程,并在此线程仍处于挂起状态时中断此线程。For example, you can start another thread that waits for a period of time and then interrupts this thread if this thread is still suspended.

适用于