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 的理解的更多相关文章

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

    转载自:http://www.cnblogs.com/easyfrog/p/3141269.html using System; using System.Collections.Generic; u ...

  2. 关于Control.Dispatcher.BeginInvoke卡界面

    Control.Dispatcher.BeginInvoke里的逻辑由UI线程执行,如果内部包含耗时操作就会造成界面卡住. Action.BeginInvoke里的逻辑,将在一个新开的线程中执行,而不 ...

  3. c#委托中的同步和异步方法即BeginInvoke和EndInvoke

    学习多线程之前我们先了解一下电脑的一些概念,比如进程,线程,这个参考https://www.cnblogs.com/loverwangshan/p/10409755.html 这篇文章.今天我们接着来 ...

  4. 控制反转(Inversion of Control)之我的理解

    关于控制反转(Inversion of Control),在具体实现上也有许多其它的叫法,如依赖倒置(Dependency Inversion Principles, DIP).依赖注入(Depend ...

  5. C#中委托、匿名函数、Lambda表达式的一些个人理解

    0x01定义一个委托,相当于定义一个可以存储方法的特殊变量类型 下面我们看具体的代码,通过代码更好理解 delegate void IntMethodInvoker(int x); 这行代码就是声明一 ...

  6. C#中Invoke 和 BeginInvoke 的区别

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

  7. 控件的invoke和beginInvoke方法

    System.Windows.Forms.Timer 的timer是在主线程上执行的,因此在timer的tick事件中操作界面上的控件不会发生线程的安全性检测. Control的invoke和begi ...

  8. C#委托同步异步说明,并比较control调用Invoke和BeginInvoke的异同

    一.委托的同步和异步: 1.同步 使用Invoke调用同步,或直接写fun1("func"),在fun1.Invoke这一步会明显的阻塞线程 使用: static void Mai ...

  9. BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题

    BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题 原文:http://www.sufeinet.com/thread-3707-1-1.html      大家可以先看看我上 ...

随机推荐

  1. 关于FragmentManager findFragmentById 返回nul

    先看Fragment的两种生成方式 一.用xml标签生成 在fragment的宿主activity中添加xml标签 <fragment android:id="@+id/fragmen ...

  2. C#条件编译,发布多平台和多种选择性的项目

    http://www.cnblogs.com/chengulv/p/4579528.html 界面操作参考 这样正对不同环境就可以编译出不同的exe或者dll,做到一个项目的灵活多变.条件编译还可以满 ...

  3. 刀哥多线程GCD核心概念gcd

    GCD GCD 核心概念 将任务添加到队列,并且指定执行任务的函数 任务使用 block 封装 任务的 block 没有参数也没有返回值 执行任务的函数 异步 dispatch_async 不用等待当 ...

  4. oracle 分析函数(笔记)

    分析函数是oracle数据库在9i版本中引入并在以后版本中不断增强的新函数种类.分析函数提供好了跨行.多层次聚合引用值的能力.分析函数所展现的效果使用传统的SQL语句也能实现,但是实现方式比较复杂,效 ...

  5. Tomcat 服务器服务的注册修改删除

    1. 注册Tomcat服务 运行cmd,切换目录到tomcat/bin, 执行以下命令service.bat install 2.删除Tomcat服务

  6. NFC framework

    NFC framework introduce 1 NFC简介 对于NFC,是google在android4.0上推出来的,简单介绍下.近场通讯(NFC)是一系列短距离无线技术,一般需要4cm或者更短 ...

  7. SVN中的常见错误(长期更新)

    一 svn中的简写含义. A:add,新增 C:conflict,冲突 D:delete,删除 M:modify,本地已经修改 G:modify and merGed,本地文件修改并且和服务器的进行合 ...

  8. .NET DES 加密

    using System; using System.Collections.Generic; using System.Text; using System.Security; using Syst ...

  9. ajax 例子

    引用 function initCsvModel(year,cityId){ var args = new Object(); args.url= "listUpload!initCsvMo ...

  10. 微软职位内部推荐-Software Engineer II-SDP

    微软近期Open的职位: Position: SDE II The R&D of Shared Data Platform at Application and Services Group ...