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 ...
随机推荐
- 对称性——cf405d
以后碰到这种题就应该往对称性想:设x的对称数x‘是1e6-x+1 对于任意一组对称数x+x'-2=1e6-1,2e6-(x+x')=1e6-1,即X集合Y集合同时加上任意一组对称数都是可以的 枚举每个 ...
- 12.RabbitMQ多机集群
配置两台Linux CentOS 6.7虚拟主机 CentOS6.7下载地址 https://pan.baidu.com/s/1i5GPg9n 安装视频下载 https://pan.baidu.c ...
- Dubbo入门到精通学习笔记(五):持续集成管理平台之sonarqube代码质量管理平台的介绍与安装
文章目录 SonarQube的介绍 SonarQube的安装 安装简介 详细安装过程 详细使用过程 SonarQube的介绍 SonarQube是一个管理代码质量的开放平台. 可以从七个维度检测代码质 ...
- 1、什么是cookie?
什么是cookie? Cookie 定义 “Cookie”是小量信息,由网络服务器发送出来以存储在网络浏览器上,从而下次这位独一无二的访客又回到该网络服务器时,可从该浏览器读回此信息.这是很有用 ...
- Java的核心优势
Java为消费类智能电子产品而设计,但智能家电产品并没有像最初想象的那样拥有大的发展.然而90年代,Internet却进入了爆发式发展阶段,一夜之间,大家都在忙着将自己的计算机连接到网络上.这个时侯, ...
- netty源码分析 - Recycler 对象池的设计
目录 一.为什么需要对象池 二.使用姿势 2.1 同线程创建回收对象 2.2 异线程创建回收对象 三.数据结构 3.1 物理数据结构图 3.2 逻辑数据结构图(重要) 四.源码分析 4.2.同线程获取 ...
- 8.1_springboot2.x之Actuator应用监控
1.监管端点测试 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- Centos下的 docker安装
安装一些必要的系统工具:sudo yum install -y yum-utils device-mapper-persistent-data lvm2 添加软件源信息:sudo yum-config ...
- iOS字符串固定间隔换行
字符串固定宽度自动换行,之前一直做是没有问题的,可能是这次的字体有些特殊.导致固定宽度下每行的字符个数不一致. 所以每两个字符之间添加换行符 //去除, NSString *name = [theme ...
- userdel -删除使用者帐号及相关档案
总览 SYNOPSIS userdel [-r] login 描述 userdel 命 令 修 改 系 统 帐 号 档 删 除 所 有 login 会 参 考 的 部 份 . 使 用 者 名 称 必 ...