GC.WaitForFullGCApproach 方法

定义

返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

重载

WaitForFullGCApproach()

返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

WaitForFullGCApproach(Int32)

在指定的超时期限内,返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

WaitForFullGCApproach()

返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

public:
 static GCNotificationStatus WaitForFullGCApproach();
[System.Security.SecurityCritical]
public static GCNotificationStatus WaitForFullGCApproach ();
static member WaitForFullGCApproach : unit -> GCNotificationStatus
Public Shared Function WaitForFullGCApproach () As GCNotificationStatus

返回

已注册垃圾回收通知的状态。The status of the registered garbage collection notification.

属性

示例

下面的示例演示如何使用此方法来确定完全、阻止的垃圾回收是否接近。The following example shows how to use this method to determine whether a full, blocking garbage collection is approaching. 只要 Succeeded通知的状态,就会调用用户方法 OnFullGCApproachNotify 来执行操作,以响应接近的集合。Whenever the status of the notification is Succeeded, the user method OnFullGCApproachNotify is called to perform actions in response to the approaching collection. 此代码示例是为垃圾回收通知主题提供的更大示例的一部分。This code example is part of a larger example provided for Garbage Collection Notifications topic.

// Check for a notification of an approaching collection.
GCNotificationStatus s = GC::WaitForFullGCApproach();
if (s == GCNotificationStatus::Succeeded)
{
    Console::WriteLine("GC Notifiction raised.");
    OnFullGCApproachNotify();
}
else if (s == GCNotificationStatus::Canceled)
{
    Console::WriteLine("GC Notification cancelled.");
    break;
}
else
{
    // This can occur if a timeout period
    // is specified for WaitForFullGCApproach(Timeout)
    // or WaitForFullGCComplete(Timeout)
    // and the time out period has elapsed.
    Console::WriteLine("GC Notification not applicable.");
    break;
}
// Check for a notification of an approaching collection.
GCNotificationStatus s = GC.WaitForFullGCApproach();
if (s == GCNotificationStatus.Succeeded)
{
    Console.WriteLine("GC Notification raised.");
    OnFullGCApproachNotify();
}
else if (s == GCNotificationStatus.Canceled)
{
    Console.WriteLine("GC Notification cancelled.");
    break;
}
else
{
    // This can occur if a timeout period
    // is specified for WaitForFullGCApproach(Timeout) 
    // or WaitForFullGCComplete(Timeout)  
    // and the time out period has elapsed. 
    Console.WriteLine("GC Notification not applicable.");
    break;
}
' Check for a notification of an approaching collection.
Dim s As GCNotificationStatus = GC.WaitForFullGCApproach
If (s = GCNotificationStatus.Succeeded) Then
    Console.WriteLine("GC Notification raised.")
    OnFullGCApproachNotify()
ElseIf (s = GCNotificationStatus.Canceled) Then
    Console.WriteLine("GC Notification cancelled.")
    Exit While
Else
    ' This can occur if a timeout period
    ' is specified for WaitForFullGCApproach(Timeout) 
    ' or WaitForFullGCComplete(Timeout)  
    ' and the time out period has elapsed. 
    Console.WriteLine("GC Notification not applicable.")
    Exit While
End If

注解

使用此方法返回的 GCNotificationStatus 枚举确定使用 RegisterForFullGCNotification 方法注册的当前垃圾回收通知的状态。Use the GCNotificationStatus enumeration returned by this method to determine the status of the current garbage collection notification that was registered by using the RegisterForFullGCNotification method. 你还可以使用 WaitForFullGCComplete 方法来确定完整垃圾回收是否已完成。You can also use the WaitForFullGCComplete method to determine whether the full garbage collection has completed.

如果枚举返回 Succeeded,则可以执行一些任务,如阻止分配其他对象并使用 Collect 方法自行引入集合。When the enumeration returns Succeeded, you can do tasks such as preventing additional objects from being allocated and inducing a collection yourself with the Collect method. 请注意,通知不保证会发生完整的垃圾回收,只有该条件达到了阈值,才能进行完整垃圾回收。Note that the notification does not guarantee that a full garbage collection will occur, only that conditions have reached the threshold that are favorable for a full garbage collection to occur.

此方法无限期地等待获取垃圾回收通知。This method waits indefinitely for a garbage collection notification to be obtained. 如果要为在无法获取通知时返回的方法指定超时时间,请使用 GC.WaitForFullGCApproach(Int32) 方法重载。If you want to specify a time-out period for the method to return if the notification cannot be obtained, use the GC.WaitForFullGCApproach(Int32) method overload. 如果在不指定超时的情况下调用此方法,则可以调用 CancelFullGCNotification 方法(如果等待时间长于首选)。If you call this method without specifying a time-out, you can call the CancelFullGCNotification method if you are waiting longer than preferred.

在调用 WaitForFullGCComplete 方法时,应遵循此方法,以确保具有完全垃圾回收。You should follow this method with a call to the WaitForFullGCComplete method to make sure that you have had a full garbage collection. 单独调用此方法会导致不确定的结果。Calling this method alone causes indeterminate results.

安全性

LinkDemand
完全信任直接调用方。for full trust for the immediate caller. 部分信任的程式碼無法使用此成員。This member cannot be used by partially trusted code.

SecurityCriticalAttribute
需要对直接调用方的完全信任。requires full trust for the immediate caller. 部分受信任的或透明的代码不能使用此成员。This member cannot be used by partially trusted or transparent code.

另请参阅

WaitForFullGCApproach(Int32)

在指定的超时期限内,返回已注册通知的状态,用于确定公共语言运行时是否即将引发完整、阻碍性垃圾回收。Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

public:
 static GCNotificationStatus WaitForFullGCApproach(int millisecondsTimeout);
[System.Security.SecurityCritical]
public static GCNotificationStatus WaitForFullGCApproach (int millisecondsTimeout);
static member WaitForFullGCApproach : int -> GCNotificationStatus
Public Shared Function WaitForFullGCApproach (millisecondsTimeout As Integer) As GCNotificationStatus

参数

millisecondsTimeout
Int32

在获取通知状态前等待的时间长度。The length of time to wait before a notification status can be obtained. 指定 -1 表示无限期等待。Specify -1 to wait indefinitely.

返回

已注册垃圾回收通知的状态。The status of the registered garbage collection notification.

属性

异常

millisecondsTimeout 必须为非负数,或者小于等于 MaxValue 或 -1。millisecondsTimeout must be either non-negative or less than or equal to MaxValue or -1.

注解

使用此方法返回的 GCNotificationStatus 枚举确定使用 RegisterForFullGCNotification 方法注册的当前垃圾回收通知的状态。Use the GCNotificationStatus enumeration returned by this method to determine the status of the current garbage collection notification that was registered by using the RegisterForFullGCNotification method. 你还可以使用 WaitForFullGCComplete 方法来确定完整垃圾回收是否已完成。You can also use the WaitForFullGCComplete method to determine whether the full garbage collection has completed.

请注意,只要获取了垃圾回收通知状态,此方法就会立即返回,而不管 millisecondsTimeout指定的值是什么。Note that this method returns immediately whenever a garbage collection notification status is obtained, regardless of the value specified by millisecondsTimeout. 如果在 millisecondsTimeout 超时之前未获得垃圾回收通知状态,则此方法返回 NotApplicableIf a garbage collection notification status is not obtained before millisecondsTimeout times out, this method returns NotApplicable.

如果枚举返回 Succeeded,则可以执行一些任务,如阻止分配其他对象并使用 Collect 方法自行引入集合。When the enumeration returns Succeeded, you can do tasks such as preventing additional objects from being allocated and inducing a collection yourself with the Collect method. 请注意,通知不保证会发生完整的垃圾回收,只有该条件达到了阈值,才能进行完整垃圾回收。Note that the notification does not guarantee that a full garbage collection will occur, only that conditions have reached the threshold that are favorable for a full garbage collection to occur.

当你无法等待超时期限结束时,你可以调用 CancelFullGCNotification 方法。You can call the CancelFullGCNotification method when you cannot wait for the time-out period to elapse.

在调用 WaitForFullGCComplete 方法时,应遵循此方法,以确保具有完全垃圾回收。You should follow this method with a call to the WaitForFullGCComplete method to make sure that you have had a full garbage collection. 单独调用此方法会导致不确定的结果。Calling this method alone causes indeterminate results.

安全性

LinkDemand
完全信任直接调用方。for full trust for the immediate caller. 部分信任的程式碼無法使用此成員。This member cannot be used by partially trusted code.

SecurityCriticalAttribute
需要对直接调用方的完全信任。requires full trust for the immediate caller. 部分受信任的或透明的代码不能使用此成员。This member cannot be used by partially trusted or transparent code.

另请参阅

适用于