Dispatcher.BeginInvoke 方法

定義

Dispatcher 關聯的執行緒上以非同步方式執行委派。

多載

BeginInvoke(Delegate, Object[])

在其中有建立 Dispatcher 的執行緒上,使用指定的引數非同步地執行指定的委派。

BeginInvoke(DispatcherPriority, Delegate)

Dispatcher 關聯的執行緒上使用指定優先權以非同步方式執行指定的委派。

BeginInvoke(Delegate, DispatcherPriority, Object[])

在其中有建立 Dispatcher 的執行緒上,依據指定的優先順序,使用指定的引數非同步地執行指定的委派。

BeginInvoke(DispatcherPriority, Delegate, Object)

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以非同步方式執行指定的委派。

BeginInvoke(DispatcherPriority, Delegate, Object, Object[])

Dispatcher 關聯的執行緒上使用指定優先權和指定引數陣列以非同步方式執行指定的委派。

備註

在 WPF 中,只有建立 的 DispatcherObject 執行緒可以存取該物件。 例如,從主要 UI 執行緒擷取的背景執行緒無法更新在 UI 執行緒上建立的 Button 內容。 為了讓背景執行緒存取 的 Button Content 屬性,背景執行緒必須將工作委派給 Dispatcher 與 UI 執行緒相關聯的 。 這是使用 InvokeBeginInvoke 來完成。 Invoke 是同步的,而且 BeginInvoke 是非同步。 作業會新增至位於指定 DispatcherPriority 之 的事件佇列 Dispatcher 中。

BeginInvoke 為非同步;因此,控制項會在呼叫物件之後立即傳回給呼叫物件。

BeginInvoke 會傳 DispatcherOperation 回 物件,當委派位於事件佇列中時,可用來與委派互動。

DispatcherOperationBeginInvoke 回的物件可以使用數種方式來與指定的委派互動,例如:

  • 在事件佇列中擱置執行時變更 DispatcherPriority 委派的 。

  • 從事件佇列移除委派。

  • 等候委派傳回。

  • 取得委派在執行之後傳回的值。

如果多個 BeginInvoke 呼叫是在同 DispatcherPriority 一個 進行,則會依呼叫的循序執行。

如果在 BeginInvoke 已關閉的 上 Dispatcher 呼叫 ,則傳 DispatcherOperation 回的狀態屬性會設定為 Aborted

BeginInvoke(Delegate, Object[])

在其中有建立 Dispatcher 的執行緒上,使用指定的引數非同步地執行指定的委派。

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(Delegate ^ method, ... cli::array <System::Object ^> ^ args);
public System.Windows.Threading.DispatcherOperation BeginInvoke (Delegate method, params object[] args);
member this.BeginInvoke : Delegate * obj[] -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (method As Delegate, ParamArray args As Object()) As DispatcherOperation

參數

method
Delegate

接受 args 中指定之參數之方法的委派,它會被推入至 Dispatcher 事件佇列。

args
Object[]

做為引數傳遞至指定方法的物件陣列。 可以是 null

傳回

DispatcherOperation

物件,此物件是在呼叫 BeginInvoke 之後立即傳回的,可用來與在事件佇列中暫止執行的委派進行互動。

備註

DispatcherOperationBeginInvoke 回的物件可以使用數種方式來與指定的委派互動,例如:

  • 在事件佇列中擱置執行時變更 DispatcherPriority 委派的 。

  • 從事件佇列移除委派。

  • 等候委派傳回。

  • 取得委派在執行之後傳回的值。

BeginInvoke 為非同步;因此,控制項會在呼叫物件之後立即傳回給呼叫物件。

在 WPF 中,只有建立 的 DispatcherObject 執行緒可以存取該物件。 例如,從主要 UI 執行緒擷取的背景執行緒無法更新在 UI 執行緒上建立的 Button 內容。 為了讓背景執行緒存取 的 Button Content 屬性,背景執行緒必須將工作委派給 Dispatcher 與 UI 執行緒相關聯的 。 這是使用 InvokeBeginInvoke 來完成。 Invoke 是同步的,而且 BeginInvoke 是非同步。 作業會新增至位於指定 DispatcherPriority 之 的事件佇列 Dispatcher 中。

如果在 BeginInvoke 已關閉的 上 Dispatcher 呼叫 ,則傳 DispatcherOperation 回的狀態屬性會設定為 Aborted

適用於

BeginInvoke(DispatcherPriority, Delegate)

Dispatcher 關聯的執行緒上使用指定優先權以非同步方式執行指定的委派。

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(System::Windows::Threading::DispatcherPriority priority, Delegate ^ method);
[System.ComponentModel.Browsable(false)]
public System.Windows.Threading.DispatcherOperation BeginInvoke (System.Windows.Threading.DispatcherPriority priority, Delegate method);
[<System.ComponentModel.Browsable(false)>]
member this.BeginInvoke : System.Windows.Threading.DispatcherPriority * Delegate -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (priority As DispatcherPriority, method As Delegate) As DispatcherOperation

參數

priority
DispatcherPriority

相對於事件佇列中 Dispatcher 其他暫止作業的優先順序,叫用指定的 方法。

method
Delegate

未採用任何引數的方法委派,此委派會推入至 Dispatcher 事件佇列。

傳回

DispatcherOperation

物件,此物件是在呼叫 BeginInvoke 之後立即傳回的,可用來與在事件佇列中暫止執行的委派進行互動。

屬性

例外狀況

methodnull

priority 不是有效的 DispatcherPriority

範例

下列範例示範如何將作業 Dispatcher 放在 上。 如需此範例的完整原始程式碼,請參閱 具有Long-Running計算範例的單一線程應用程式

首先,會建立接受無引數的委派。

public delegate void NextPrimeDelegate();
Public Delegate Sub NextPrimeDelegate()

接下來會 BeginInvoke(DispatcherPriority, Delegate) 呼叫 。 因為每個 DispatcherObject 屬性都會傳回 Dispatcher 它相關聯的 屬性,所以會藉由查詢 DispatcherObject 來取得所需的 Dispatcher ,在此案例中為 Button 名為 startStopButton 的 。 的呼叫 BeginInvoke(DispatcherPriority, Delegate) 會採用兩個參數:優先順序,其設定 DispatcherPriority.Normal 為 ,而回呼則是透過委派 NextPrimeDelegate 的實例傳入。

startStopButton.Dispatcher.BeginInvoke(
    DispatcherPriority.Normal,
    new NextPrimeDelegate(CheckNextNumber));
startStopButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New NextPrimeDelegate(AddressOf CheckNextNumber))

備註

如果多個 BeginInvoke 呼叫是在同 DispatcherPriority 一個 進行,則會依呼叫的循序執行。

BeginInvoke 會傳 DispatcherOperation 回 物件,當委派位於事件佇列中時,可用來與委派互動。

DispatcherOperationBeginInvoke 回的物件可以使用數種方式來與指定的委派互動,例如:

  • 在事件佇列中擱置執行時變更 DispatcherPriority 委派的 。

  • 從事件佇列移除委派。

  • 等候委派傳回。

  • 取得委派在執行之後傳回的值。

BeginInvoke 為非同步;因此,控制項會在呼叫物件之後立即傳回給呼叫物件。

在 WPF 中,只有建立 的 DispatcherObject 執行緒可以存取該物件。 例如,從主要 UI 執行緒擷取的背景執行緒無法更新在 UI 執行緒上建立的 Button 內容。 為了讓背景執行緒存取 的 Button Content 屬性,背景執行緒必須將工作委派給 Dispatcher 與 UI 執行緒相關聯的 。 這是使用 InvokeBeginInvoke 來完成。 Invoke 是同步的,而且 BeginInvoke 是非同步。 作業會新增至位於指定 DispatcherPriority 之 的事件佇列 Dispatcher 中。

如果在 BeginInvoke 已關閉的 上 Dispatcher 呼叫 ,則傳 DispatcherOperation 回的狀態屬性會設定為 Aborted

另請參閱

適用於

BeginInvoke(Delegate, DispatcherPriority, Object[])

在其中有建立 Dispatcher 的執行緒上,依據指定的優先順序,使用指定的引數非同步地執行指定的委派。

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(Delegate ^ method, System::Windows::Threading::DispatcherPriority priority, ... cli::array <System::Object ^> ^ args);
public System.Windows.Threading.DispatcherOperation BeginInvoke (Delegate method, System.Windows.Threading.DispatcherPriority priority, params object[] args);
member this.BeginInvoke : Delegate * System.Windows.Threading.DispatcherPriority * obj[] -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (method As Delegate, priority As DispatcherPriority, ParamArray args As Object()) As DispatcherOperation

參數

method
Delegate

接受 args 中指定之參數之方法的委派,它會被推入至 Dispatcher 事件佇列。

priority
DispatcherPriority

相對於事件佇列中 Dispatcher 其他暫止作業的優先順序,叫用指定的 方法。

args
Object[]

做為引數傳遞至指定方法的物件陣列。 可以是 null

傳回

DispatcherOperation

物件,此物件是在呼叫 BeginInvoke 之後立即傳回的,可用來與在事件佇列中暫止執行的委派進行互動。

備註

DispatcherOperationBeginInvoke 回的物件可以使用數種方式來與指定的委派互動,例如:

  • 在事件佇列中擱置執行時變更 DispatcherPriority 委派的 。

  • 從事件佇列移除委派。

  • 等候委派傳回。

  • 取得委派在執行之後傳回的值。

BeginInvoke 為非同步;因此,控制項會在呼叫物件之後立即傳回給呼叫物件。

在 WPF 中,只有建立 的 DispatcherObject 執行緒可以存取該物件。 例如,從主要 UI 執行緒擷取的背景執行緒無法更新在 UI 執行緒上建立的 Button 內容。 為了讓背景執行緒存取 的 Button Content 屬性,背景執行緒必須將工作委派給 Dispatcher 與 UI 執行緒相關聯的 。 這是使用 InvokeBeginInvoke 來完成。 Invoke 是同步的,而且 BeginInvoke 是非同步。 作業會加入至位於指定 DispatcherPriority 之 的事件 Dispatcher 佇列中。

如果在 BeginInvoke 已關閉的 上 Dispatcher 呼叫 ,則傳 DispatcherOperation 回的狀態屬性會設定為 Aborted

適用於

BeginInvoke(DispatcherPriority, Delegate, Object)

Dispatcher 關聯的執行緒上使用指定優先權和指定引數以非同步方式執行指定的委派。

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(System::Windows::Threading::DispatcherPriority priority, Delegate ^ method, System::Object ^ arg);
[System.ComponentModel.Browsable(false)]
public System.Windows.Threading.DispatcherOperation BeginInvoke (System.Windows.Threading.DispatcherPriority priority, Delegate method, object arg);
[<System.ComponentModel.Browsable(false)>]
member this.BeginInvoke : System.Windows.Threading.DispatcherPriority * Delegate * obj -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (priority As DispatcherPriority, method As Delegate, arg As Object) As DispatcherOperation

參數

priority
DispatcherPriority

相對於事件佇列中 Dispatcher 叫用指定方法之其他暫止作業的優先順序。

method
Delegate

採用一個引數的方法委派,此委派會推入至 Dispatcher 事件佇列。

arg
Object

做為引數傳遞至指定方法的物件。

傳回

DispatcherOperation

物件,此物件是在呼叫 BeginInvoke 之後立即傳回的,可用來與在事件佇列中暫止執行的委派進行互動。

屬性

例外狀況

methodnull

priority 不是有效的 DispatcherPriority

範例

下列範例示範如何將作業 Dispatcher 放在 上。

首先,會建立接受一個引數的委派,在此案例中為字串。

private delegate void OneArgDelegate(String arg);
Private Delegate Sub OneArgDelegate(ByVal arg As String)

接下來, BeginInvoke(DispatcherPriority, Delegate, Object) 會呼叫 。 因為每個 DispatcherObject 都有傳回 Dispatcher 其關聯的 屬性,所以會藉由查詢 DispatcherObject 來取得所需的 Dispatcher ,在此案例中為 Grid 名為 tomorrowsWeather 的 。 的 BeginInvoke(DispatcherPriority, Delegate, Object) 呼叫會採用三個參數:優先順序,其設定為 DispatcherPriority.Normal ;回呼,其會透過委派 OneArgDelegate 的實例傳入;以及名為 weather 的字串,這是回呼的引數。

// Schedule the update function in the UI thread.
tomorrowsWeather.Dispatcher.BeginInvoke(
    System.Windows.Threading.DispatcherPriority.Normal,
    new OneArgDelegate(UpdateUserInterface), 
    weather);
' Schedule the update function in the UI thread.
tomorrowsWeather.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New OneArgDelegate(AddressOf UpdateUserInterface), weather)

備註

arg 如果不需要引數,則可以是 null

BeginInvokeDispatcherOperation 回 物件,當委派位於事件佇列中時,可用來與委派互動。

DispatcherOperationBeginInvoke 回的物件可以透過數種方式與指定的委派互動,例如:

  • DispatcherPriority 事件佇列中擱置執行時變更委派的 。

  • 從事件佇列中移除委派。

  • 等候委派傳回。

  • 取得委派在執行之後傳回的值。

BeginInvoke 是非同步;因此,控制項會在呼叫物件之後立即傳回給呼叫物件。

在 WPF 中,只有建立 的 DispatcherObject 執行緒可以存取該物件。 例如,從主要 UI 執行緒擷取的背景執行緒無法更新在 UI 執行緒上建立的 Button 內容。 為了讓背景執行緒存取 的內容 Button 屬性,背景執行緒必須將工作委派給 Dispatcher 與 UI 執行緒相關聯的 。 這是使用 InvokeBeginInvoke 來完成。 Invoke 是同步的,而且 BeginInvoke 是非同步。 作業會加入至位於指定 DispatcherPriority 之 的事件 Dispatcher 佇列中。

如果多個 BeginInvoke DispatcherPriority 呼叫在同一處進行,則會依呼叫的進行循序執行。

如果在 BeginInvoke 已關閉的 上 Dispatcher 呼叫 ,則傳 DispatcherOperation 回的狀態屬性會設定為 Aborted

另請參閱

適用於

BeginInvoke(DispatcherPriority, Delegate, Object, Object[])

Dispatcher 關聯的執行緒上使用指定優先權和指定引數陣列以非同步方式執行指定的委派。

public:
 System::Windows::Threading::DispatcherOperation ^ BeginInvoke(System::Windows::Threading::DispatcherPriority priority, Delegate ^ method, System::Object ^ arg, ... cli::array <System::Object ^> ^ args);
[System.ComponentModel.Browsable(false)]
public System.Windows.Threading.DispatcherOperation BeginInvoke (System.Windows.Threading.DispatcherPriority priority, Delegate method, object arg, params object[] args);
[<System.ComponentModel.Browsable(false)>]
member this.BeginInvoke : System.Windows.Threading.DispatcherPriority * Delegate * obj * obj[] -> System.Windows.Threading.DispatcherOperation
Public Function BeginInvoke (priority As DispatcherPriority, method As Delegate, arg As Object, ParamArray args As Object()) As DispatcherOperation

參數

priority
DispatcherPriority

相對於事件佇列中 Dispatcher 叫用指定方法之其他暫止作業的優先順序。

method
Delegate

採用多個引數的方法委派,此委派會推入至 Dispatcher 事件佇列。

arg
Object

做為引數傳遞至指定方法的物件。

args
Object[]

做為引數傳遞至指定方法的物件陣列。

傳回

DispatcherOperation

物件,此物件是在呼叫 BeginInvoke 之後立即傳回的,可用來與在 Dispatcher 佇列中暫止執行的委派進行互動。

屬性

例外狀況

methodnull

DispatcherPriority 不是有效的優先權。

備註

如果不需要引數,則 arg 參數可以是 null

BeginInvokeDispatcherOperation 回 物件,當委派位於事件佇列中時,可用來與委派互動。

DispatcherOperationBeginInvoke 回的物件可以透過數種方式與指定的委派互動,例如:

  • DispatcherPriority 事件佇列中擱置執行時變更委派的 。

  • 從事件佇列中移除委派。

  • 等候委派傳回。

  • 取得委派在執行之後傳回的值。

BeginInvoke 是非同步;因此,控制項會在呼叫物件之後立即傳回給呼叫物件。

在 WPF 中,只有建立 的 DispatcherObject 執行緒可以存取該物件。 例如,從主要 UI 執行緒擷取的背景執行緒無法更新在 UI 執行緒上建立的 Button 內容。 為了讓背景執行緒存取 的 Button Content 屬性,背景執行緒必須將工作委派給 Dispatcher 與 UI 執行緒相關聯的 。 這是使用 InvokeBeginInvoke 來完成。 Invoke 是同步的,而且 BeginInvoke 是非同步。 作業會加入至位於指定 DispatcherPriority 之 的事件 Dispatcher 佇列中。

如果多個 BeginInvoke DispatcherPriority 呼叫在同一處進行,則會依呼叫的進行循序執行。

如果在 BeginInvoke 已關閉的 上 Dispatcher 呼叫 ,則傳 DispatcherOperation 回的狀態屬性會設定為 Aborted

另請參閱

適用於