心如止水
Dispatcher.Invoke and Dispatcher.BeginInvokeDispatcher.Invoke是同步執(zhí)行,msdn描述: Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with. 返回值是object, 是被調(diào)用的委托的返回值,如果該委托沒有返回值,則為null。它有好幾個重載方法,下面是其中之一: public Object Invoke( Delegate method, paramsObject[] args ) Invoke is a synchronous operation; therefore, control will not return to the calling object until after the callback returns. --------------------------------------------------------------------------------------------------------------------------------- Dispatcher.BeginInvoke是異步執(zhí)行,msdn描述: Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. 返回值是DispatcherOperation, An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the delegate as it is pending execution in the event queue. 它有好幾個重載方法,下面是其中之一: public DispatcherOperation BeginInvoke( Delegate method, params Object[] args ) 不同點是Dispatcher.Invoke直到回調(diào)返回之后才將控制權(quán)返回給調(diào)用對象。Dispatcher.BeginInvoke是立即返回控制權(quán)給調(diào)用對象,只是把delegate加到消息循環(huán)中。
如下面的例子:
輸出結(jié)果: Invoke 1 分類: WPF |
|