共用方式為


GC.WaitForPendingFinalizers 方法

定義

暫止目前的執行緒,直到處理完成項佇列的執行緒已經清空該佇列為止。

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

範例

下列範例示範如何使用 WaitForPendingFinalizers 方法來暫停目前的執行緒,直到完成所有收集的物件完成為止。

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);
       }
        }
    }
}
open System

// You can increase this number to fill up more memory.
let numMfos = 1000

// You can increase this number to cause more
// post-finalization work to be done.
let maxIterations = 100

[<AllowNullLiteral>]
type MyFinalizeObject() =
    // Make this number very large to cause the finalizer todo more work.
    let maxIterations = 10000

    override _.Finalize() =
        printfn "Finalizing a MyFinalizeObject"
        
        // Do some work.
        for i = 1 to maxIterations do
            // This method performs no operation on i, but prevents
            // the JIT compiler from optimizing away the code inside
            // the loop.
            GC.KeepAlive i

let mutable mfo = null

// Create and release a large number of objects
// that require finalization.
for j = 1 to numMfos do
    mfo <- 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 _ = 1 to maxIterations do
    printfn "Doing some post-finalize work"
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

備註

當垃圾收集行程找到可回收的物件時,它會檢查每個物件,以判斷物件的最終處理需求。 如果物件實作完成項,而且尚未藉由呼叫 SuppressFinalize 來停用最終處理,物件就會放在標示為已準備好進行最終處理的物件清單中。 垃圾收集行程會 Finalize 呼叫此清單中物件的 方法,並從清單中移除專案。 這個方法會封鎖,直到所有完成項都執行完成為止。

執行完成項的執行緒未指定,因此不保證此方法將會終止。 不過,當方法正在進行時 WaitForPendingFinalizers ,這個執行緒可能會被另一個執行緒中斷。 例如,您可以啟動另一個等候一段時間的執行緒,然後在此執行緒仍暫停時中斷此執行緒。

適用於