Marshal.CleanupUnusedObjectsInCurrentContext 方法

定義

通知執行階段清除目前內容中配置的所有執行階段可呼叫包裝函式 (RCW)

public:
 static void CleanupUnusedObjectsInCurrentContext();
public static void CleanupUnusedObjectsInCurrentContext ();
[System.Security.SecurityCritical]
public static void CleanupUnusedObjectsInCurrentContext ();
static member CleanupUnusedObjectsInCurrentContext : unit -> unit
[<System.Security.SecurityCritical>]
static member CleanupUnusedObjectsInCurrentContext : unit -> unit
Public Shared Sub CleanupUnusedObjectsInCurrentContext ()
屬性

備註

Interop 系統會在嘗試清除 RCW 時幫浦訊息。 這可確保所有應用程式中都會發生單線程 Apartment (STA) RCW 的清除,包括不常幫浦的應用程式。 不過,某些適當地進行幫浦的應用程式會非常敏感于發生幫浦的位置。 當 Interop 系統在這些應用程式中擷取訊息時,在非預期的地方,應用程式可能會遇到難以診斷的重新進入問題。 因此,應用程式必須能夠控制自己的特定 STA 幫浦,以及控制 RCW 的清除。

不過,開發人員通常只會控制其應用程式中的執行緒子集,因此無法確保所有線程都適當地提取。

若要解決此問題:

  1. Thread.DisableComObjectEagerCleanup使用 方法來關閉 RCW 的自動清除,以及每一線程發生的訊息幫浦。 這可讓開發人員退出宣告自動清除,以及對應的訊息幫浦。

  2. CleanupUnusedObjectsInCurrentContext使用 方法來通知執行時間,以清除目前內容中配置的所有 RCW。 這個隨附方法可讓開發人員精確地控制執行時間在目前內容中執行清除的時機。

適用於