zoukankan      html  css  js  c++  java
  • .c# Invoke和BeginInvoke 区别

    Control.Invoke 方法 (Delegate):拥有此控件的基础窗口句柄的线程上执行指定的委托。

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

    以下为实际应用中碰到的问题,在主线程中启动一个线程,然后在这个线程中启动serviceForm,然而在线程启动后,往serviceForm发送指令,serviceForm.IsHandleCreated老是报serviceForm = null,无法执行指令,采用延时的办法可以解决此问题,但不是高效的办法,后来在serviceForm.Load += new EventHandler(serviceForm_Load);serviceForm_Load事件中添加指令,发送成功。主要原因还是多线程所致。

    SatirServiceForm serviceForm;
            Thread serviceFormThread;

    protected void Init()
            {
                serviceFormThread = new Thread(MainFormMessageThread);
                serviceFormThread.Name = "ServiceThread";
                serviceFormThread.Start();
            }

    protected void MainFormMessageThread()
            {
                if (serviceForm == null)
                {
                    serviceForm = new SatirServiceForm();
                    serviceForm.Load += new EventHandler(serviceForm_Load);
                    //serviceForm.RecvedCmd += new EventHandler(OnServiceRecvedCmd);

                }
                Application.Run(serviceForm);
            }

    void serviceForm_Load(object sender, EventArgs e)
            {
                SendCommand(InfraOnlineCmd.Start, 0);
                SendCommand(InfraOnlineCmd.AutoAjust, 0);
            }

    protected override void SendCommand(InfraOnlineCmd cmd, object param)
            {
                if (param != null && param is int)
                    this.param = (int)param;

          if (serviceForm.IsHandleCreated)
                {
                    serviceForm.BeginInvoke(new DCmdHandler(ExecuteCmd), cmd);
                }
            }

    以下ZT From:http://blog.163.com/kjpt126@126/blog/static/48940426200824103658846/

    Control的Invoke和BeginInvoke

    近日,被Control的Invoke和BeginInvoke搞的头大,就查了些相关的资料,整理如下。感谢这篇文章对我的理解Invoke和BeginInvoke的真正含义 。

    (一)Control的Invoke和BeginInvoke

    我们要基于以下认识:

    (1)Control的Invoke和BeginInvoke与Delegate的Invoke和BeginInvoke是不同的。

    (2)Control的Invoke和BeginInvoke的参数为delegate,委托的方法是在Control的线程上执行的,也就是我们平时所说的UI线程。

    我们以代码(一)来看(Control的Invoke)

    private delegate void InvokeDelegate();

    private void InvokeMethod(){

        //C代码段

    }

    private void butInvoke_Click(object sender, EventArgs e) {

        //A代码段.......

        this.Invoke(new InvokeDelegate(InvokeMethod));

        //B代码段......

    }

    你觉得代码的执行顺序是什么呢?记好Control的Invoke和BeginInvoke都执行在主线程即UI线程上

    A------>C---------------->B

    解释:(1)A在UI线程上执行完后,开始Invoke,Invoke是同步

    (2)代码段B并不执行,而是立即在UI线程上执行InvokeMethod方法,即代码段C。

    (3)InvokeMethod方法执行完后,代码段C才在UI线程上继续执行。

    看看代码(二),Control的BeginInvoke

    private delegate void BeginInvokeDelegate();

    private void BeginInvokeMethod(){

        //C代码段

    }

    private void butBeginInvoke_Click(object sender, EventArgs e) {

        //A代码段.......

        this.BeginInvoke(new BeginInvokeDelegate(BeginInvokeMethod));

        //B代码段......

    }

    你觉得代码的执行顺序是什么呢?记好Control的Invoke和BeginInvoke都执行在主线程即UI线程上

    A----------->B--------------->C慎重,这个只做参考。。。。。,我也不肯定执行顺序,如果有哪位达人知道的话请告知。

    解释::(1)A在UI线程上执行完后,开始BeginInvoke,BeginInvoke是异步

    (2)InvokeMethod方法,即代码段C不会执行,而是立即在UI线程上执行代码段B。

    (3)代码段B执行完后(就是说butBeginInvoke_Click方法执行完后),InvokeMethod方法,即代码段C才在UI线程上继续执行。

    由此,我们知道:

    Control的Invoke和BeginInvoke的委托方法是在主线程,即UI线程上执行的。也就是说如果你的委托方法用来取花费时间长的数据,然后更新界面什么的,千万别在UI线程上调用Control.Invoke和Control.BeginInvoke,因为这些是依然阻塞UI线程的,造成界面的假死。

    那么,这个异步到底是什么意思呢?

    异步是指相对于调用BeginInvoke的线程异步,而不是相对于UI线程异步,你在UI线程上调用BeginInvoke ,当然不行了。----摘自"Invoke和BeginInvoke的真正涵义"一文中的评论。

    BeginInvoke的原理是将调用的方法Marshal成消息,然后调用Win32 API中的RegisterWindowMessage()向UI窗口发送消息。----摘自"Invoke和BeginInvoke的真正涵义"一文中的评论。

    (二)我们用Thread来调用BeginInvoke和Invoke

           我们开一个线程,让线程执行一些耗费时间的操作,然后再用Control.Invoke和Control.BeginInvoke回到用户UI线程,执行界面更新。

    代码(三) Thread调用Control的Invoke

    private Thread invokeThread;

    private delegate void invokeDelegate();

    private void StartMethod(){

       //C代码段......

        Control.Invoke(new invokeDelegate(invokeMethod));

    //D代码段......

    }

    private void invokeMethod(){

    //E代码段

    }

    private void butInvoke_Click(object sender, EventArgs e) {

        //A代码段.......

       invokeThread = new Thread(new ThreadStart(StartMethod));

       invokeThread.Start();

        //B代码段......

    }

    你觉得代码的执行顺序是什么呢?记好Control的Invoke和BeginInvoke都执行在主线程即UI线程上

    A------>(Start一开始B和StartMethod的C就同时执行)---->(C执行完了,不管B有没有执行完,invokeThread把消息封送(invoke)给UI线程,然后自己等待)---->UI线程处理完butInvoke_Click消息后,处理invokeThread封送过来的消息,执行invokeMethod方法,即代码段E,处理往后UI线程切换到invokeThread线程。

    这个Control.Invoke是相对于invokeThread线程同步的,阻止了其运行。

    解释:

    1。UI执行A

    2。UI开线程InvokeThread,B和C同时执行,B执行在线程UI上,C执行在线程invokeThread上。

    3。invokeThread封送消息给UI,然后自己等待,UI处理完消息后,处理invokeThread封送的消息,即代码段E

    4。UI执行完E后,转到线程invokeThread上,invokeThread线程执行代码段D

    代码(四) Thread调用Control的BeginInvoke

    private Thread beginInvokeThread;

    private delegate void beginInvokeDelegate();

    private void StartMethod(){

       //C代码段......

        Control.BeginInvoke(new beginInvokeDelegate(beginInvokeMethod));

    //D代码段......

    }

    private void beginInvokeMethod(){

    //E代码段

    }

    private void butBeginInvoke_Click(object sender, EventArgs e) {

        //A代码段.......

       beginInvokeThread = new Thread(new ThreadStart(StartMethod));

       beginInvokeThread .Start();

        //B代码段......

    }

    你觉得代码的执行顺序是什么呢?记好Control的Invoke和BeginInvoke都执行在主线程即UI线程上

    A在UI线程上执行----->beginInvokeThread线程开始执行,UI继续执行代码段B,并发地invokeThread执行代码段C-------------->不管UI有没有执行完代码段B,这时beginInvokeThread线程把消息封送给UI,单自己并不等待,继续向下执行-------->UI处理完butBeginInvoke_Click消息后,处理beginInvokeThread线程封送过来的消息。

    解释:

    1。UI执行A

    2。UI开线程beginInvokeThread,B和C同时执行,B执行在线程UI上,C执行在线程beginInvokeThread上。

    3。beginInvokeThread封送消息给UI,然后自己继续执行代码D,UI处理完消息后,处理invokeThread封送的消息,即代码段E

    有点疑问:如果UI先执行完毕,是不是有可能过了段时间beginInvokeThread才把消息封送给UI,然后UI才继续执行封送的消息E。如图浅绿的部分。

    Control的BeginInvoke是相对于调用它的线程,即beginInvokeThread相对是异步的。

    因此,我们可以想到。如果要异步取耗费长时间的数据,比如从数据库中读大量数据,我们应该这么做。

    (1)如果你想阻止调用线程,那么调用代码(三),代码段D删掉,C改为耗费长时间的操作,因为这个操作是在另外一个线程中做的。代码段E改为更新界面的方法。

    (2)如果你不想阻止调用线程,那么调用代码(四),代码段D删掉,C改为耗费长时间的操作,因为这个操作是在另外一个线程中做的。代码段E改为更新界面的方法。

  • 相关阅读:
    pywin32解析office文档
    解决NGUI自动被设置LYAER
    ngui的tween的tweenFactor属性
    ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)
    unity调用摄像头的方法
    坐标转换,这次是反过来,屏幕坐标转换成世界坐标
    unity5.3 安卓广告插件打包出错的理解
    从世界坐标转换成ui的rect坐标的方法
    关于unity碰撞检测器的用法
    测试第一篇标题
  • 原文地址:https://www.cnblogs.com/jcomet/p/2665503.html
Copyright © 2011-2022 走看看