zoukankan      html  css  js  c++  java
  • 转: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);
            }
        }
    }
  • 相关阅读:
    修改带!important的css样式
    解决Eclipse导入项目是提示错误:Some projects cannot be imported because they already exist in the workspace
    HTML5——canvas:使用画布绘制卡片
    vue:更改组件样式
    导入导出大量excel文件
    winfrom控件Treeview绑定数据库中的资料(节点控件)
    Winfrom将excel中的数据导入sqlserver数据库中的方法
    C# 将DataTable表中的数据批量插入到数据库表中的方法
    创建Sql数据表的sql代码
    Winfrom之SplitContainer控件
  • 原文地址:https://www.cnblogs.com/lusunqing/p/3197716.html
Copyright © 2011-2022 走看看