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 ...
随机推荐
- NOIP2019模拟2019.9.20】膜拜大会(外向树容斥,分类讨论)
传送门. 题解: 我果然是不擅长分类讨论,心态被搞崩了. 注意到\(m<=n-2\),意味着除了1以外的位置不可能被加到a[1]两遍. 先考虑个大概: 考虑若存在\(x,x-1,-,2\)(有序 ...
- 浅析阿里云API网关的产品架构和常见应用场景
自上世纪60年代计算机网络发展开始,API(Application Programming Interface )随之诞生,API即应用程序接口,是实现系统间衔接的桥梁.时至今日,API市场已经形成了 ...
- OpenSearch最新功能介绍
摘要:阿里云开放搜索(OpenSearch)是一款结构化数据搜索托管服务,其能够提供简单.高效.稳定.低成本和可扩展的搜索解决方案.OpenSearch以平台服务化的形式,将专业搜索技术简单化.低门槛 ...
- Github代码管理教程
https://desktop.github.com/ 目录 Create and use a repository Start and manage a new branch Make change ...
- delphi dll创建及调用
第一章 DLL简单介绍由于在目前的学习工作中,需要用到DLL文件,就学习了下,在这里作个总结.首先装简单介绍下DLL:1,减小可执行文件的大小DLL技术的产生有很大一部分原因是为了减小可执行文件的大小 ...
- 38-Ubuntu-用户管理-03-usermod指定用户登录shell
简记: 所谓shell就是可以输入终端命令的窗口,shell是一个软件. 1.Ubuntu终端shell介绍 summmer@summmer-virtual-machine:~/桌面$ summmer ...
- 随笔记录 误删boot恢复 2019.8.7
系统还原: 1. 2. 3. 4. 5.进入硬盘 6.挂载光盘,安装恢复boot 7.安装grub2 8.重建grub.cfg文件
- linux 挂载iso文件,挂载ntfs文件系统
映像档不可录就挂载使用.通过loop命令来执行 好吧.跟同事要了一个win10系统盘.插入,竟然是灰色的. ,一点击,提示无法挂载,仔细看了一下下面的内容,原来不支持ntfs格式,好吧,win10系统 ...
- JVM配置参数理解,Cannot load this JVM TI agent twice
基本参数 -Xms128m JVM初始分配的堆内存 -Xmx512m JVM最大允许分配的堆内存,按需分配 -XX:PermSize=64M JVM初始分配的非堆内存 -XX:MaxPermSize= ...
- Vue开发实战
递归组件 关键是组件在模板内能调用自身,关键是name属性 首先我们先定义数据格式 list: [ { title: '标题1' }, { title: '标题2', children: [ { ti ...