转:C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解

转载自:http://www.cnblogs.com/easyfrog/p/3141269.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace InvokeTest
{
    public partial class Form1 : Form
    {
        public Form1() {
            InitializeComponent();
        }

        /**
         *  对 委托 的 BeginInvoke,EndInvoke 及 Control 的 BeginInvoke,EndInvoke 的理解: 
         * 
         *  委托的 BeginInvoke,是使用 异步的方式. 它的参数1 是传入一个完成方法后调用的回调函数
         *  参数2,是像 这个回调函数里传入的一个 Object 参数.
         *  
         *  EndInvoke ,是得到 委托方法的返回值. 如果委托方法没有返回值.那就只需要一个用来完成
         *  后回调的方法即可. 如果有返回值的方法. 还是很有必要使用这个 EndInvoke 的.
         *  
         *  Control 上的 Invoke 及 BeginInvoke 都是运行在 UI 线程上的,所以都会造成 界面阻塞.
         *  我们一般是使用委托的 BeginInvoke 去异步执行一些耗时方法.在 执行完后 的回调中使用
         *  Control.Invoke 方法去更新UI界面 如: 
         *      this.Invoke(new Action(() => this.button1.Text = (string)ar.AsyncState));
         */

        // 如果调用 BeginInvoke 的方法没有返回值的话,只是在方法结束后调用一个 回调函数的话
        // 则在 BeginInvoke 的时候就不必传入委托自身. 当然也不需要调用 EndInvoke 方法,得到
        // 返回值了.
        private void Form1_Load(object sender, EventArgs e) {
            // 异步
            (new Action(() => { Thread.Sleep(3000); })).BeginInvoke(new AsyncCallback((ar) => {
                this.Invoke(new Action(() => this.button1.Text = (string)ar.AsyncState));
            }), "KAOKAO");
        }


        // 如果你调用 BeginInvoke 的委托方法是有返回值的,那么你就需要在调用BeginInvoke的时候,把
        // 委托的本身做为第二个参数传到 CallBack 函数中,在里面 通过 把 AsyncState 强转为我们的委托
        // 类型, 然后就可以使用委托的 EndInvoke 方法,这个方法返回的就是 我们的委托函数所返回的值.
        private void button1_Click(object sender, EventArgs e) {
            // Func
            Func<float> Func = new Func<float>(() => { Thread.Sleep(1000); return 123.456f; });

            Func.BeginInvoke(new AsyncCallback((ar) => {
                Func<float> tmp = (Func<float>)ar.AsyncState;
                float tmpint = tmp.EndInvoke(ar);
                this.Invoke(new Action(() => {
                    this.label1.Text = tmpint.ToString();
                }));
            }), Func);
        }
    }
}

 

posted @ 2013-07-18 10:55  LS庆  阅读(901)  评论(0编辑  收藏  举报