IAsyncResult
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的更多相关文章
- 基于ASP.NET的comet简单实现 http长连接,IAsyncResult
http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html 我潜水很多年,今天忽然出现.很久没写过博客了,不是因为不想写,而是 ...
- IAsyncResult 接口异步 和匿名委托
IAsyncResult 接口异步 DataSet ds = new DataSet(); Mydelegate del = new Mydelegate(LoadData); IAsyncResul ...
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
- 异步核心接口IAsyncResult的实现
要实现异步编程,就需要正确的实现IAsyncResult接口.IAsyncResult共有四个属性: public interface IAsyncResult { object AsyncState ...
- IAsyncResult 接口
IAsyncResult 接口由包含可异步操作的方法的类实现.它是启动异步操作的方法的返回类型,如 FileStream.BeginRead,也是结束异步操作的方法的第三个参数的类型,如 FileSt ...
- IAsyncResult接口
#region 程序集 mscorlib.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framewor ...
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...
- C#中的异步调用及异步设计模式(二)——基于 IAsyncResult 的异步设计模式
三.基于 IAsyncResult 的异步设计模式(设计层面) IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来 ...
- .net IAsyncResult 异步操作
//定义一个委托 public delegate int DoSomething(int count); //BeginInvoke 的回调函数 private static void Execute ...
- C#异步编程模式IAsyncResult
IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 FileStream 类提供了 B ...
随机推荐
- Java-Class-C:org.springframework.util.Assert
ylbtech-Java-Class-C:org.springframework.util.Assert 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 1. /* * Copyr ...
- ()获取Cookies session
[HttpGet] public string mo() { var httpRequest = HttpContext.Current.Request; var a = httpRequest.Co ...
- 2.3 Gulp
在前端工程化中最重要的就是流程管理,借用 gulp 可以很方便的基于流的方式定义流程任务,并将任务串联起来,本节中将详细介绍 gulp ,包括: gulp 介绍 gulp 是什么 gulp 能够解决哪 ...
- ISA虚拟化的条件
ISA(Instruction Set Architecture) 指令集体系结构,是硬件与软件层之间的接口. 本地系统虚拟机 本地系统虚拟机,就是Bare-Metal虚拟机,直接运行在硬件上,在它上 ...
- Codeforces451A-Game With Sticks-思维
After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are pl ...
- vue-cli中进行微信支付代码详解
最近做微信支付,颇经历一番波折,这里总结一下,便于以后少走弯路: 在进行微信支付,除了需要公众号之外,你还需要一个微信商户.根据商户规则进行商户申请 这是公众号的基本开发配置,这里在微信授权的时候就已 ...
- 初识 flex 布局
开启弹性盒模式: display:flex / inline-flex: inline-flex 行内弹性盒 1.设置 flex 缩放的 限定值 min-width 最小值 min-wi ...
- Linux 父子进程实现复制文件内容到另一个文件内
1. 子进程先拷贝前一半 ,父进程一直阻塞到子进程拷贝完再拷贝后一半 /* 子进程先拷贝前一半文件,父进程先阻塞等待子进程拷贝完前一半内容, * 然后父进程在拷贝,后一半内容 * */ #includ ...
- 18_ShadowWalker
白皮书中 page-fault error code: shadowWalker 原理: 接管 指定程序 的 执行页面异常.读写页面异常:然后 调用一下正常的 使其出现在快表:然后恢复到假的pte - ...
- Nginx网站部署
Nginx网站服务部署 常用的网站服务软件 处理静态资源的服务: apache软件:https://apache.org/ nginx软件:https://nginx.org/ 处理动态资源的服务: ...