转载自: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(); })).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(); 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);
}
}
}

最新文章

  1. Linux的概念与体系
  2. 虚拟机VMware 12 Pro 永久许可证激活密钥
  3. C#:USB设备枚举 --转自CSDN作者:Splash
  4. loadrunner中定义数组
  5. 在Windows2003上安装Active Directory Management Gateway Service
  6. yii在TbGridView的td里面加入相应的下拉选项(转)
  7. (六)6.4 Neurons Networks Autoencoders and Sparsity
  8. 配置LINUX为路由
  9. 【Mysql知识补充】
  10. 转载NodePort,LoadBalancer还是Ingress?我该如何选择 - kubernetes
  11. css实用属性
  12. Python内置函数(45)——object
  13. 安装anaconda和python3.7环境
  14. 使用session和cookie实现用户登录:一个登录页面,一个servlet,一个登录成功页面
  15. python css选择器
  16. Caused by: java.lang.ClassNotFoundException: Could not load requested class :XXX.XXX.XXX 异常处理
  17. oracle中实现md5加密
  18. Configure Pi as simulation hardware for Simulink
  19. java8 - 多线程时间安全问题
  20. Could not find any resources for the specified culture or the neutral culture

热门文章

  1. WARNING: firstResult/maxResults specified with collection fetch; applying in memory!
  2. (4.24)sql server变量中set与select的区别
  3. w命令 查看系统负载
  4. 对比损失(Contrastive Loss)学习【转载】
  5. network error:software caused connection abort
  6. Struts2漏洞利用工具下载(更新2017-V1.8版增加S2-045/S2-046)
  7. python-对象与参数传递
  8. Linux下利用Valgrind工具进行内存泄露检测和性能分析
  9. for in //for of //forEach //map三种对比
  10. selenium自定义find_element