C#_delegate - 异步调用实例 BeginInvoke EndInvoke event
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace EventClock
{ public class ClassWithDelegate
{
//封装了一个返回值为int的多重委托方法
public delegate int DelegateThatReturns(); public event DelegateThatReturns theDelegate; public void Run()
{
for (; ; )
{
Thread.Sleep(500); if (theDelegate != null)
{
//显式调用每个委托方法
foreach (DelegateThatReturns del in theDelegate.GetInvocationList())
{
//异步调用
//传入委托作为一个状态对象
del.BeginInvoke(new AsyncCallback(ResultReturns), del); }
Console.WriteLine();
}
}
}
//获取结果的回调方法
public void ResultReturns(IAsyncResult iar)
{
//将状态对象转换为委托类型
DelegateThatReturns del = (DelegateThatReturns)iar.AsyncState;
//调用委托的EndInvoke方法获取结果
int result = del.EndInvoke(iar);
//显示结果
Console.WriteLine("Delegate returned result: {0}",result);
}
} public class FirstSubscribe
{
private int myCounter = 0; public void Subscribe(ClassWithDelegate theClassWithDelegate)
{
theClassWithDelegate.theDelegate += new ClassWithDelegate.DelegateThatReturns(DisplayCounter);
} public int DisplayCounter()
{
Console.WriteLine("Busy in displaying............");
Thread.Sleep(10000);
Console.WriteLine("Do not with work in display............");
return ++myCounter;
}
}
public class SecondSubscribe
{
private int myCounter = 0; public void Subscribe(ClassWithDelegate theClassWithDelegate)
{
theClassWithDelegate.theDelegate += new ClassWithDelegate.DelegateThatReturns(Doubler);
} public int Doubler()
{
return myCounter += 2;
}
} class Program
{
static void Main(string[] args)
{
ClassWithDelegate theClassWithDelegate = new ClassWithDelegate(); FirstSubscribe fs = new FirstSubscribe();
fs.Subscribe(theClassWithDelegate); SecondSubscribe ss = new SecondSubscribe();
ss.Subscribe(theClassWithDelegate); theClassWithDelegate.Run(); Console.ReadLine();
}
}
}
C#_delegate - 异步调用实例 BeginInvoke EndInvoke event的更多相关文章
- .NET中的async和await关键字使用及Task异步调用实例
其实早在.NET 4.5的时候M$就在.NET中引入了async和await关键字(VB为Async和Await)来简化异步调用的编程模式.我也早就体验过了,现在写一篇日志来记录一下顺便凑日志数量(以 ...
- C#如何使用异步编程【BeginInvoke/EndInvoke】
怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...
- java实现异步调用实例
在JAVA平台,实现异步调用的角色有如下三个角色: 调用者 取货凭证 真实数据 一个调用者在调用耗时操作,不能立即返回数据时,先返回一个取货凭证.然后在过一断时间后凭取货凭证来获取真正的数据. ...
- 异步和多线程,委托异步调用,Thread,ThreadPool,Task,Parallel,CancellationTokenSource
1 进程-线程-多线程,同步和异步2 异步使用和回调3 异步参数4 异步等待5 异步返回值 5 多线程的特点:不卡主线程.速度快.无序性7 thread:线程等待,回调,前台线程/后台线程, 8 th ...
- C#“同步调用”、“异步调用”、“异步回调”
本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: ); //模拟该方法运 ...
- C#委托的异步调用1
本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: /*添加的命名空间 u ...
- C#委托的异步调用
本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: /*添加的命名空间 u ...
- 整理 C#(同步调用、异步调用、异步回调)
//闲来无事,巩固同步异步方面的知识,以备后用,特整理如下: class Program { static void Main(string[] args) { //同步调用 会阻塞当前线程,一步一步 ...
- C#委托的异步调用【转】
本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: ); //模拟该方法运 ...
随机推荐
- apache开源项目--Apache Drill
为了帮助企业用户寻找更为有效.加快Hadoop数据查询的方法,Apache 软件基金会发起了一项名为“Drill”的开源项目.Apache Drill 实现了 Google's Dremel. Apa ...
- 深入理解Arrays.sort()
两种方法: 1.类本来就实现java.lang.Comparable接口,使类本身就有比较能力.接口实现compareTo方法,次方法接收另一个Object为参数,如果当前对象小于参数则返回负值,如果 ...
- SharePoint 2010 master page 控件介绍(1)
转:http://blog.csdn.net/lgm97/article/details/6409204 以下所有的内容都是根据Randy Drisgill (MVP SharePoint Serve ...
- 微信公众平台开发—利用OAuth2.0获取微信用户基本信息
在借鉴前两篇获取微信用户基本信息的基础下,本人也总结整理了一些个人笔记:如何通过OAuth2.0获取微信用户信息 1.首先在某微信平台下配置OAuth2.0授权回调页面: 2.通过appid构造url ...
- (Android Studio)添加按钮以及权重问题
此文大部分摘自http://hukai.me/android-training-course-in-chinese/basics/firstapp/building-ui.html 注意到光标所在代码 ...
- 关于T公司的强矩阵架构的思考
我所在的T公司是强矩阵架构,关于这类公司,应该是不少大公司的主流架构,也就是说一个职员在公司内不仅在项目内有相应的级别,在其行政上也是有相应的级别,日常工作以项目的内容为主,但是同时也是属于行政的一员 ...
- matlab 函数说明—ordfilt2
今天看harris角点实现的源码,在某一个版本中看到了这个函数,不是很理解,doc ordfilt2之后还是不清楚,终于在matlab论坛上搞清楚了ordfilt2的功能. 中文理解函数名就是顺序 ...
- 【转】shell中IFS用法
http://blog.itpub.net/27181165/viewspace-775820/ 一 IFS的介绍 Shell 脚本中有个变量叫IFS(Internal Field Seprato ...
- HW6.7
public class Solution { public static void main(String[] args) { int[] count = new int[10]; int numb ...
- C语言的格式控制符
1. 格式控制符 格式输出printf 作用是向终端输出若干个类型任意的数据. 格式:printf (格式控制符,输出列表) 1) 格式控制符 l % ...