Dispatcher.BeginInvoke 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在所 Dispatcher 關聯的執行緒上非同步執行代理。
多載
| 名稱 | Description |
|---|---|
| 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 執行緒分拆出來的背景執行緒無法更新 Button 在 UI 執行緒上建立的內容。 為了讓背景執行緒存取 的內容屬性 Button,背景執行緒必須將工作委派給與 UI 執行緒相關的執行 Dispatcher 緒。 此操作可透過使用或 InvokeBeginInvoke來達成。 Invoke 是同步的,也是 BeginInvoke 非同步的。 該操作會被加入指定位置DispatcherPriority的Dispatcher事件佇列中。
BeginInvoke 是非同步的;因此,控制權在被呼叫物件後立即回傳。
BeginInvoke 回傳 DispatcherOperation 一個物件,當代理在事件佇列中時,可以用來與該代理互動。
DispatcherOperation回傳的BeginInvoke物件可以用多種方式與指定的代理子互動,例如:
在事件佇列中代理正在等待執行時,改變 。DispatcherPriority
將代理從事件佇列中移除。
等待代表回來。
取得代理執行後回傳的值。
若同時進行DispatcherPriority多BeginInvoke通電話,則會依通話順序執行。
若 BeginInvoke 在已關機的 上 Dispatcher 被呼叫,則 returnd 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。
傳回
一個物件會被呼叫,該物件會在事件 BeginInvoke 隊列中等待執行時立即回傳,該物件可用來與代理互動。
備註
DispatcherOperation回傳的BeginInvoke物件可以用多種方式與指定的代理子互動,例如:
在事件佇列中代理正在等待執行時,改變 。DispatcherPriority
將代理從事件佇列中移除。
等待代表回來。
取得代理執行後回傳的值。
BeginInvoke 是非同步的;因此,控制權在被呼叫物件後立即回傳。
在 WPF 中,只有建立 的 DispatcherObject 執行緒才能存取該物件。 例如,從主 UI 執行緒分拆出來的背景執行緒無法更新 Button 在 UI 執行緒上建立的內容。 為了讓背景執行緒存取 的內容屬性 Button,背景執行緒必須將工作委派給與 UI 執行緒相關的執行 Dispatcher 緒。 此操作可透過使用或 InvokeBeginInvoke來達成。 Invoke 是同步的,也是 BeginInvoke 非同步的。 該操作會被加入指定位置DispatcherPriority的Dispatcher事件佇列中。
若 BeginInvoke 在已關機的 上 Dispatcher 被呼叫,則 returnd 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
傳回
一個物件會被呼叫,該物件會在事件 BeginInvoke 隊列中等待執行時立即回傳,該物件可用來與代理互動。
- 屬性
例外狀況
method 為 null。
priority 不是有效的 DispatcherPriority。
範例
以下範例說明如何將操作置於 Dispatcher。 關於此範例的完整原始碼,請參見 Single-Threaded 應用與 Long-Running 計算範例。
首先,會產生一個代表,不接受任何辯論。
public delegate void NextPrimeDelegate();
Public Delegate Sub NextPrimeDelegate()
接著, BeginInvoke(DispatcherPriority, Delegate) 是被叫出的。 由於每個 DispatcherObject 都有一個返回 Dispatcher 其所關聯的屬性,因此 desired Dispatcher 是透過查詢 DispatcherObject,在此例中為 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))
備註
若同時進行DispatcherPriority多BeginInvoke通電話,則會依通話順序執行。
BeginInvoke 回傳 DispatcherOperation 一個物件,當代理在事件佇列中時,可以用來與該代理互動。
DispatcherOperation回傳的BeginInvoke物件可以用多種方式與指定的代理子互動,例如:
在事件佇列中代理正在等待執行時,改變 。DispatcherPriority
將代理從事件佇列中移除。
等待代表回來。
取得代理執行後回傳的值。
BeginInvoke 是非同步的;因此,控制權在被呼叫物件後立即回傳。
在 WPF 中,只有建立 的 DispatcherObject 執行緒才能存取該物件。 例如,從主 UI 執行緒分拆出來的背景執行緒無法更新 Button 在 UI 執行緒上建立的內容。 為了讓背景執行緒存取 的內容屬性 Button,背景執行緒必須將工作委派給與 UI 執行緒相關的執行 Dispatcher 緒。 此操作可透過使用或 InvokeBeginInvoke來達成。 Invoke 是同步的,也是 BeginInvoke 非同步的。 該操作會被加入指定位置DispatcherPriority的Dispatcher事件佇列中。
若 BeginInvoke 在已關機的 上 Dispatcher 被呼叫,則 returnd 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。
傳回
一個物件會被呼叫,該物件會在事件 BeginInvoke 隊列中等待執行時立即回傳,該物件可用來與代理互動。
備註
DispatcherOperation回傳的BeginInvoke物件可以用多種方式與指定的代理子互動,例如:
在事件佇列中代理正在等待執行時,改變 。DispatcherPriority
將代理從事件佇列中移除。
等待代表回來。
取得代理執行後回傳的值。
BeginInvoke 是非同步的;因此,控制權在被呼叫物件後立即回傳。
在 WPF 中,只有建立 的 DispatcherObject 執行緒才能存取該物件。 例如,從主 UI 執行緒分拆出來的背景執行緒無法更新 Button 在 UI 執行緒上建立的內容。 為了讓背景執行緒存取 的內容屬性 Button,背景執行緒必須將工作委派給與 UI 執行緒相關的執行 Dispatcher 緒。 此操作可透過使用或 InvokeBeginInvoke來達成。 Invoke 是同步的,也是 BeginInvoke 非同步的。 該操作會被加入指定位置DispatcherPriority的Dispatcher事件佇列中。
若 BeginInvoke 在已關機的 上 Dispatcher 被呼叫,則 returnd 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
將物件傳遞為指定方法的參數。
傳回
一個物件會被呼叫,該物件會在事件 BeginInvoke 隊列中等待執行時立即回傳,該物件可用來與代理互動。
- 屬性
例外狀況
method 為 null。
priority 不是有效的 DispatcherPriority。
範例
以下範例說明如何將操作置於 Dispatcher。
首先,建立一個代理,接受一個參數,在此例中為一個字串。
private delegate void OneArgDelegate(String arg);
Private Delegate Sub OneArgDelegate(ByVal arg As String)
接著, BeginInvoke(DispatcherPriority, Delegate, Object) 是被叫出的。 由於每個 DispatcherObject 都有一個返回 Dispatcher 其所關聯的屬性,因此 desired Dispatcher 是透過查詢 DispatcherObject,在此例中為 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
BeginInvoke 回傳 DispatcherOperation 一個物件,當代理在事件佇列中時,可以用來與該代理互動。
DispatcherOperation回傳的BeginInvoke物件可以用多種方式與指定的代理子互動,例如:
在事件佇列中代理正在等待執行時,改變 。DispatcherPriority
將代理從事件佇列中移除。
等待代表回來。
取得代理執行後回傳的值。
BeginInvoke 是非同步的;因此,控制權在被呼叫物件後立即回傳。
在 WPF 中,只有建立 的 DispatcherObject 執行緒才能存取該物件。 例如,從主 UI 執行緒分拆出來的背景執行緒無法更新 Button 在 UI 執行緒上建立的內容。 為了讓背景執行緒存取 的內容屬性 Button,背景執行緒必須將工作委派給與 UI 執行緒相關的執行 Dispatcher 緒。 此操作可透過使用或 InvokeBeginInvoke來達成。 Invoke 是同步的,也是 BeginInvoke 非同步的。 該操作會被加入指定位置DispatcherPriority的Dispatcher事件佇列中。
若同時進行DispatcherPriority多BeginInvoke通電話,則會依通話順序執行。
若 BeginInvoke 在已關機的 上 Dispatcher 被呼叫,則 returnd 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[]
要當做自變數傳遞至指定方法的物件陣列。
傳回
一個物件在呼叫後 BeginInvoke 立即回傳,可用於在等待執行 Dispatcher 的代理中與代理互動。
- 屬性
例外狀況
method 為 null。
DispatcherPriority 不是一個有效的優先事項。
備註
arg參數可以null是,若不需要參數。
BeginInvoke 回傳 DispatcherOperation 一個物件,當代理在事件佇列中時,可以用來與該代理互動。
DispatcherOperation回傳的BeginInvoke物件可以用多種方式與指定的代理子互動,例如:
在事件佇列中代理正在等待執行時,改變 。DispatcherPriority
將代理從事件佇列中移除。
等待代表回來。
取得代理執行後回傳的值。
BeginInvoke 是非同步的;因此,控制權在被呼叫物件後立即回傳。
在 WPF 中,只有建立 的 DispatcherObject 執行緒才能存取該物件。 例如,從主 UI 執行緒分拆出來的背景執行緒無法更新 Button 在 UI 執行緒上建立的內容。 為了讓背景執行緒存取 的內容屬性 Button,背景執行緒必須將工作委派給與 UI 執行緒相關的執行 Dispatcher 緒。 此操作可透過使用或 InvokeBeginInvoke來達成。 Invoke 是同步的,也是 BeginInvoke 非同步的。 該操作會被加入指定位置DispatcherPriority的Dispatcher事件佇列中。
若同時進行DispatcherPriority多BeginInvoke通電話,則會依通話順序執行。
若 BeginInvoke 在已關機的 上 Dispatcher 被呼叫,則 returnd DispatcherOperation 的狀態屬性設為 Aborted。