Control.InvokeRequired 屬性

定義

取得一個值。這個值會指示是否由於呼叫端是在建立控制項之執行緒以外的執行緒,因此在進行控制項的方法呼叫時,應呼叫叫用 (Invoke) 方法。Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on.

public:
 property bool InvokeRequired { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool InvokeRequired { get; }
member this.InvokeRequired : bool
Public ReadOnly Property InvokeRequired As Boolean

屬性值

如果控制項的 true 是在呼叫執行緒以外的執行緒上建立,則為 Handle (表示必須透過叫用方法呼叫),否則為 falsetrue if the control's Handle was created on a different thread than the calling thread (indicating that you must make calls to the control through an invoke method); otherwise, false.

實作

屬性

備註

Windows Forms 中的控制項系結至特定的執行緒,而且不是安全線程。Controls in Windows Forms are bound to a specific thread and are not thread safe. 因此,如果您從不同的執行緒呼叫控制項的方法,就必須使用其中一個控制項的 invoke 方法,將呼叫封送處理至適當的執行緒。Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. 這個屬性可用來判斷您是否必須呼叫叫用方法,如果您不知道哪個執行緒擁有控制項,這就很有用。This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control.

注意

除了 InvokeRequired 屬性以外,控制項上還有四種方法可供呼叫,以執行緒安全的方式呼叫: InvokeBeginInvokeEndInvoke,以及 CreateGraphics (如果已建立控制項的控制碼)。In addition to the InvokeRequired property, there are four methods on a control that are thread safe to call: Invoke,BeginInvoke, EndInvoke and CreateGraphics if the handle for the control has already been created. 在背景執行緒上建立控制項的控制碼之前呼叫 CreateGraphics,可能會造成不合法的跨執行緒呼叫。Calling CreateGraphics before the control's handle has been created on a background thread can cause illegal cross thread calls. 針對所有其他方法呼叫,當從不同的執行緒呼叫時,您應該使用其中一個 invoke 方法。For all other method calls, you should use one of these invoke methods when calling from a different thread.

如果控制項的控制碼尚不存在,InvokeRequired 會在控制項的父鏈上搜尋,直到找到有視窗控制碼的控制項或表單為止。If the control's handle does not yet exist, InvokeRequired searches up the control's parent chain until it finds a control or form that does have a window handle. 如果找不到適當的控制碼,則 InvokeRequired 方法會傳回 falseIf no appropriate handle can be found, the InvokeRequired method returns false.

這表示如果不需要 Invoke (呼叫會在同一個執行緒上執行),或如果控制項是在不同的執行緒上建立,但尚未建立控制項的控制碼,則 InvokeRequired 可能會傳回 falseThis means that InvokeRequired can return false if Invoke is not required (the call occurs on the same thread), or if the control was created on a different thread but the control's handle has not yet been created.

在尚未建立控制項控制碼的情況下,您不應該只呼叫控制項上的屬性、方法或事件。In the case where the control's handle has not yet been created, you should not simply call properties, methods, or events on the control. 這可能會導致在背景執行緒上建立控制項的控制碼、隔離線程上的控制項,而不需要訊息提取並使應用程式不穩定。This might cause the control's handle to be created on the background thread, isolating the control on a thread without a message pump and making the application unstable.

InvokeRequired 在背景執行緒上傳回 false 時,您也可以藉由檢查 IsHandleCreated 的值來防範此情況。You can protect against this case by also checking the value of IsHandleCreated when InvokeRequired returns false on a background thread. 如果尚未建立控制項控制碼,您必須等到它建立完成後,再呼叫 InvokeBeginInvokeIf the control handle has not yet been created, you must wait until it has been created before calling Invoke or BeginInvoke. 一般來說,只有在應用程式的主要表單(在顯示表單或已呼叫 Application.Run 之前,在 Application.Run(new MainForm()) 中建立背景執行緒時,才會發生這種情況。Typically, this happens only if a background thread is created in the constructor of the primary form for the application (as in Application.Run(new MainForm()), before the form has been shown or Application.Run has been called.

其中一個解決方案是等到表單的控制碼建立完成後,再啟動背景執行緒。One solution is to wait until the form's handle has been created before starting the background thread. 請藉由呼叫 Handle 屬性來強制建立控制碼,或等候 Load 事件啟動背景進程。Either force handle creation by calling the Handle property, or wait until the Load event to start the background process.

更好的解決方案是使用 SynchronizationContext 所傳回的 SynchronizationContext,而不是跨執行緒封送處理的控制項。An even better solution is to use the SynchronizationContext returned by SynchronizationContext rather than a control for cross-thread marshaling.

注意

如果應該處理訊息的執行緒不再使用中,可能會擲回例外狀況。An exception might be thrown if the thread that should process the message is no longer active.

如需多執行緒 Windows Forms 控制項的詳細資訊,請參閱 How 至:使用背景執行緒來搜尋 的檔案,並 如何:對的 Windows Forms 控制項進行安全線程呼叫。For more information about multithreaded Windows Forms controls, see How to: Use a Background Thread to Search for Files and How to: Make Thread-Safe Calls to Windows Forms Controls.

適用於

另請參閱