小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Dispatcher.Invoke and Dispatcher.BeginInvoke

 牛人的尾巴 2017-11-13
心如止水

Dispatcher.Invoke and Dispatcher.BeginInvoke

Dispatcher.Invoke是同步執(zhí)行,msdn描述:

Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with.

返回值是object, 是被調(diào)用的委托的返回值,如果該委托沒有返回值,則為null。它有好幾個(gè)重載方法,下面是其中之一:

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. 它有好幾個(gè)重載方法,下面是其中之一:

public DispatcherOperation BeginInvoke(
	Delegate method,
	params Object[] args
)
不同點(diǎn)是Dispatcher.Invoke直到回調(diào)返回之后才將控制權(quán)返回給調(diào)用對象。Dispatcher.BeginInvoke是立即返回控制權(quán)給調(diào)用對象,只是把delegate加到消息循環(huán)中。
如下面的例子:
private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Test1();
            Test2();
            Test3();
        }
        private void Test1()
        {
            // 同步的按代碼順序執(zhí)行
            Dispatcher.Invoke(new Action<string>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.SystemIdle, "Invoke 1");
            Dispatcher.Invoke(new Action<string>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.Send, "Invoke 2");
            // 以下3個(gè)異步,
            Dispatcher.BeginInvoke(new Action<string>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.Normal, "BeginInvoke 1");
            Dispatcher.BeginInvoke(new Action<string>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.Send, "BeginInvoke 2");
            // 異步默認(rèn)的DispatcherPriority是Normal
            DispatcherOperation dop = Dispatcher.BeginInvoke(new Action<string>(Console.WriteLine), "BeginInvoke 3");
            Console.WriteLine("dop.Priorty: " + dop.Priority.ToString());
            Console.WriteLine("Hello normal");
        }
        private void Test2()
        {
            Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Invoke 1: Hello world")));
            MessageBox.Show("Invoke 2: Hello world");
        }
        private void Test3()
        {
            Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show("hello 1"); }), System.Windows.Threading.DispatcherPriority.Background);
            MessageBox.Show("hello 2");
        }

輸出結(jié)果:

Invoke 1
 Invoke 2
 dop.Priorty: Normal
 Hello normal
 BeginInvoke 2
 BeginInvoke 1
 BeginInvoke 3

分類: WPF

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多