C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解
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);
}
}
}
C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解的更多相关文章
- 转:C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解
转载自:http://www.cnblogs.com/easyfrog/p/3141269.html using System; using System.Collections.Generic; u ...
- 关于Control.Dispatcher.BeginInvoke卡界面
Control.Dispatcher.BeginInvoke里的逻辑由UI线程执行,如果内部包含耗时操作就会造成界面卡住. Action.BeginInvoke里的逻辑,将在一个新开的线程中执行,而不 ...
- c#委托中的同步和异步方法即BeginInvoke和EndInvoke
学习多线程之前我们先了解一下电脑的一些概念,比如进程,线程,这个参考https://www.cnblogs.com/loverwangshan/p/10409755.html 这篇文章.今天我们接着来 ...
- 控制反转(Inversion of Control)之我的理解
关于控制反转(Inversion of Control),在具体实现上也有许多其它的叫法,如依赖倒置(Dependency Inversion Principles, DIP).依赖注入(Depend ...
- C#中委托、匿名函数、Lambda表达式的一些个人理解
0x01定义一个委托,相当于定义一个可以存储方法的特殊变量类型 下面我们看具体的代码,通过代码更好理解 delegate void IntMethodInvoker(int x); 这行代码就是声明一 ...
- C#中Invoke 和 BeginInvoke 的区别
Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托. Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句 ...
- 控件的invoke和beginInvoke方法
System.Windows.Forms.Timer 的timer是在主线程上执行的,因此在timer的tick事件中操作界面上的控件不会发生线程的安全性检测. Control的invoke和begi ...
- C#委托同步异步说明,并比较control调用Invoke和BeginInvoke的异同
一.委托的同步和异步: 1.同步 使用Invoke调用同步,或直接写fun1("func"),在fun1.Invoke这一步会明显的阻塞线程 使用: static void Mai ...
- BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题
BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题 原文:http://www.sufeinet.com/thread-3707-1-1.html 大家可以先看看我上 ...
随机推荐
- 内存详解--理解 JVM 如何使用 AIX 上的本机内存
转自---http://www.ibm.com/developerworks/cn/java/j-nativememory-aix/
- Linux获取用户主目录
#!/usr/bin/python# -*- coding:utf-8 -*-import sysimport osclass get_home_path(object): def __init__( ...
- next permutaion算法
算法描述: Find largest index i such that array[i − 1] < array[i]. Find largest index j such that j ≥ ...
- 统计工具之QQ图
正态 QQ 图和普通 QQ 图 分位数-分位数 (QQ) 图是两种分布的分位数相对彼此进行绘制的图.评估数据集是否正态分布,并分别研究两个数据集是否具有相似的分布. 如何构建正态 QQ 图 首先,数据 ...
- StyleCop学习笔记——默认的规则
在StyleCop中有一些官方自己写好的检测规则下面就是英文的解释 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialEl ...
- RMAN - 备份异机恢复
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...
- GIS中栅格数据的拼接
Datamanager Tools——Raster——Raster Dataset——Mosaic to New Raster 如果最大值是实际的真值,选择masaic operator要保留Max ...
- iOS App Launch Option
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- CALayer加阴影后动画卡的处理办法
[self.layer setShadowColor:[UIColor blackColor].CGColor]; [self.layer setShadowOpacity:0.8]; [self.l ...
- Extjs-工具条和菜单 Ext.menu和Ext.Toolbar
转载自:http://blog.csdn.net/itlwc/article/details/7878002 1.创建一个简单工具条效果图 <script type="text/jav ...