zoukankan      html  css  js  c++  java
  • WPF Invoke与BeginInvoke的区别

    • Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托。
    • Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句柄所在线程上异步执行指定委托。

        就是一个是同步的一个是异步的,也就是一个需要等待一个不需要等待

    复制代码
    //这个输出123
            private void button1_Click(object sender, RoutedEventArgs e) {
                textblock.Text += "1";
                this.Dispatcher.Invoke(new InvokeDelegate(Test));
                textblock.Text += "3";
            }
    
            private void Test() {
                textblock.Text += "2";
            }
    
            private delegate void InvokeDelegate();
    
    //这个输出132
            private void button1_Click(object sender, RoutedEventArgs e) {
                textblock.Text += "1";
                this.Dispatcher.BeginInvoke(new InvokeDelegate(Test));
                textblock.Text += "3";
            }
    
            private void Test() {
                textblock.Text += "2";
            }
    复制代码

    Invoke是线程中比较重要的一个东西,在多线程的编程中,平常在更新界面的时候,可以用UI线程去做来减轻工作线程的负担。比如下面这样放在线程中:

    复制代码
    private void button1_Click(object sender, RoutedEventArgs e) {
                Thread invokeThread = new Thread(new ThreadStart(Method));
                invokeThread.Start();
                //...运算代码
            }
    
            private void Method(){
                this.Dispatcher.BeginInvoke(new InvokeDelegate(Test));
            }
    
            private void Test() {
                textblock.Text += "123";
            }
    
            private delegate void InvokeDelegate();
    复制代码

    简单写法如下:

    private void button_Click(object sender, RoutedEventArgs e) {
                this.Dispatcher.BeginInvoke(new Action(() => { this.textblock.Text += "123"; }));
            }
  • 相关阅读:
    hdu 2112 (最短路+map)
    poj 1502 最短路+坑爹题意
    poj 1696 Space Ant (极角排序)
    poj 1410 线段相交判断
    使用本地光盘安装Microsoft .NET Framework 3.5 for Win8.1/WinServer2012R2
    Excel REPT函数使用
    tomcat7配置虚拟目录
    Tomcat 7.0的配置
    js去除空格
    JAVABEAN连接各数据库
  • 原文地址:https://www.cnblogs.com/Jeely/p/11043865.html
Copyright © 2011-2022 走看看