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. APUE线程控制

    一.线程的限制 sysconf可以查看的值 PTHREAD_DESTRUCTOR_ITERATIONS 线程退出时操作系统实现试图销毁线程特定数据的最大次数 _SC_THREAD_DESTRUCTOR ...

  2. MySQL常规操作以及问题

    背景 作为一个前端,偶尔搞下后端 要熟悉 SQL 操作,但是一段时间不用 会大部分忘记,之后又要重新查资料 所以自己整理一遍经常用到的 SQL 操作 和使用过程遇到的问题,方便自己快速查阅 一.安装 ...

  3. Android 在OnCreate()中获取控件高度与宽度

    试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与getHeight()方法返回是0,具体原因 看一下Activity的生命周期 就会明白. 上代码: 方法一: int ...

  4. jdk环境配置-windows 10

    近期由于云服务器到期,重新买了一个云服务器,这里顺便把jdk环境配置步骤做一个记录 1.下载自己需要的jdk 我这里是下的免安装版的  2.计算机(此电脑)->属性->高级系统设置-> ...

  5. C语言新手写扫雷攻略1

    工欲善其事,必先利其器,首先要准备好开发环境,既然是C语言,那就不是WinAPI的扫雷,就是纯的C语言开发,但是以前的C都是TC开发的,现在用肯定是过时很久了,但是也是有解决办法的,某些大神开发出Ea ...

  6. 11、jQueryEasyUI的基本组件

    1.拖动的div <!--jquery 的主文件...--> <script type="text/javascript" src="../../js/ ...

  7. django 重写用户模型 AbstractBaseUser

    https://blog.csdn.net/weixin_40744265/article/details/80745652

  8. 【react】---Hooks的基本使用---【巷子】

    一.react-hooks概念 React中一切皆为组件,React中组件分为类组件和函数组件,在React中如果需要记录一个组件的状态的时候,那么这个组件必须是类组件.那么能否让函数组件拥有类组件的 ...

  9. 并发新构件之Exchanger:交换器

    Exchanger:JDK描述:可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchang ...

  10. Boring counting HDU - 3518 后缀自动机

    题意: 对于给出的字符串S, 长度不超过1000, 求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次 题解: 将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出 ...