using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace asyncApp
{
public partial class FrmAsync : Form
{
public FrmAsync()
{
InitializeComponent();
}
public int ExecuteTask1(int num)
{
Thread.Sleep(5000);
return num * num;
}
public int ExecuteTask2(int num)
{
return num * num;
}
private void btnExectue1_Click(object sender, EventArgs e)
{
this.lblCount1.Text = ExecuteTask1(10).ToString();
this.lblCount2.Text = ExecuteTask2(10).ToString();
} private void btnExecute2_Click(object sender, EventArgs e)
{
MyDelegate dete = ExecuteTask1;
//异步操作执行状态借口
IAsyncResult result = dete.BeginInvoke(10,null,null);
this.lblCount1.Text = "正在计算......";
this.lblCount2.Text = ExecuteTask2(10).ToString();
//EndInvoke方法借助IAsyncResult借口对象,不断地查询异步调用是否结束;
//该方法知道异步调用的方法所有参数,所以异步调用完毕以后,取出异步调用结果作为返回值
int res = dete.EndInvoke(result);
this.lblCount1.Text = res.ToString();
}
public delegate int MyDelegate(int num);
}
}

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace asyncAppCallback
{
public partial class FrmAppCallback : Form
{
public FrmAppCallback()
{
InitializeComponent();
this.objCal = new MyDelegate(ExecuteTask);//初始化成员变量
//this.objCal = (num, ms)=>
//{
// Thread.Sleep(ms);
// return num * num;
//}; }
//【1】声明一个委托
public delegate int MyDelegate(int num, int ms);
//【2】根据委托声明一个方法
private int ExecuteTask(int num, int ms)
{
Thread.Sleep(ms);
return num * num;
}
//【3】创建委托变量
MyDelegate objCal = null;//ExecuteTask;
//【4】同步执行多个任务
private void btnExecu_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
objCal.BeginInvoke(10 * i, 1000 * i, MyCallback,i);
}
}
//【5】创建回调函数
private void MyCallback(IAsyncResult result)
{
int res= objCal.EndInvoke(result);
//异步显示结果
Console.WriteLine("第{0}个计算结果:{1}",result.AsyncState,res);
}
}
}

  

IAsyncResult的更多相关文章

  1. 基于ASP.NET的comet简单实现 http长连接,IAsyncResult

    http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html 我潜水很多年,今天忽然出现.很久没写过博客了,不是因为不想写,而是 ...

  2. IAsyncResult 接口异步 和匿名委托

    IAsyncResult 接口异步 DataSet ds = new DataSet(); Mydelegate del = new Mydelegate(LoadData); IAsyncResul ...

  3. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  4. 异步核心接口IAsyncResult的实现

    要实现异步编程,就需要正确的实现IAsyncResult接口.IAsyncResult共有四个属性: public interface IAsyncResult { object AsyncState ...

  5. IAsyncResult 接口

    IAsyncResult 接口由包含可异步操作的方法的类实现.它是启动异步操作的方法的返回类型,如 FileStream.BeginRead,也是结束异步操作的方法的第三个参数的类型,如 FileSt ...

  6. IAsyncResult接口

    #region 程序集 mscorlib.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewor ...

  7. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  8. C#中的异步调用及异步设计模式(二)——基于 IAsyncResult 的异步设计模式

    三.基于 IAsyncResult 的异步设计模式(设计层面) IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来 ...

  9. .net IAsyncResult 异步操作

    //定义一个委托 public delegate int DoSomething(int count); //BeginInvoke 的回调函数 private static void Execute ...

  10. C#异步编程模式IAsyncResult

    IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 FileStream 类提供了 B ...

随机推荐

  1. 工程师技术(一):启用SELinux保护、自定义用户环境、配置IPv6地址、配置聚合连接、配置firewalld防火墙

    一.启用SELinux保护 目标: 本例要求为虚拟机 server0.desktop0 配置SELinux: 确保 SELinux 处于强制启用模式 在每次重新开机后,此设置必须仍然有效 方案: SE ...

  2. Delphi 判断某个系统服务是否存在及相关状态

    记得use WinSvc; //------------------------------------- // 获取某个系统服务的当前状态 // // return status code if s ...

  3. TESTNG听录音笔记

    1. 是什么:有了它可以管理测试用例,做数据驱动,多线程模式下case的鲍旭类型 2. 如何生成testng的xml文件 -- based on Eclipse Eclipse里装上testn插件,指 ...

  4. 画山 paint

    画山 paint 有一张大小为n*m的白纸,小R想在纸上画一片绵延的群山. 为了描述方便,我们将纸张表示在坐标系上,四个顶点的坐标分别为(0,0),(n,0),(0,m),(n,m). 小R有一只神奇 ...

  5. Golang 标准库提供的Log(一)

      原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://gotaly.blog.51cto.com/8861157/1405754 G ...

  6. 模拟栈&&模拟队列

    模拟栈:class Stack { private List list = new ArrayList( ); public void push( Object obj ) { this.list.a ...

  7. font的基本知识

    字体 你无法预料到用户是否可以访问样式表里定义的字体.所以在设置字体时,在属性后指定一个替代的字体列表是个不错的主意. 在这个字体列表的最后加上系统字体中的一个,如:serif,sans-serif, ...

  8. docker Dockerfile学习---构建redis环境

    1.创建项目目录并下载包及文件 mkdir centos_redis cd centos_redis wget http://download.redis.io/releases/redis-5.0. ...

  9. DOM 对象和jQuery对象的转换

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Oracle使用——PLSQL的中文乱码显示全是问号--Oracle查询中文乱码

      问题 这两天刚将PLSQL与Oracle配置好,可是在PLSQL中插入数据时,出现一个问题,PLSQL中的表里无法显示中文,中文无法保存.无法输出,中文在表中显示问号,如图: 原因 经过一番查证, ...