通过委托来实现异步 Delegate的BeginInvoke和EndInvoke
什么是.net的异步机制呢?
解释这个话题之前,先让我们来看看同步执行的程序
https://github.com/chucklu/Test/blob/master/DotNet4.5开发指南/并行处理和并发/异步编程模式/APM/SyncProcedure/Program.cs
static class SyncTest
{
public static void Method()
{ Console.WriteLine("SyncTest类中的Method()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
{
Console.WriteLine("SyncTest类中的Method()函数的线程托管于线程池");
}
else
{
Console.WriteLine("SyncTest类中的Method()函数的线程没有托管在线程池上");
}
}
} class Program
{
static void Main(string[] args)
{
Console.WriteLine("Program类中的Main()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
{
Console.WriteLine("Program类中的Main()函数的线程托管于线程池");
}
else
{
Console.WriteLine("Program类中的Main()函数的线程没有托管在线程池上");
}
Console.WriteLine(); SyncTest.Method();//调用静态类的静态方法 Console.Read();//阻塞,确保能看到上面的打印信息
}
}
执行结果如下图所示

从图中可以看出,在主函数中调用另外一个类SyncTest的方法,
进入方法Method的时候,此方法还是和Main函数在同一个线程上执行的
并且该线程不是托管在线程池上的线程
接下来写一个通过委托,进行异步执行的方法
https://github.com/chucklu/Test/blob/master/DotNet4.5开发指南/并行处理和并发/异步编程模式/APM/AsyncProcedure/Program.cs
不要尝试去查看BeginInvoke和EndInvoke的实现,右键,转到定义,是看不到的。这两个方法是CLR为委托类型添加的
/// <summary>
/// 声明一个委托 委托的名字是AsyncHandler
/// </summary>
/// <param name="name"></param>
public delegate void AsyncHandler();//委托的声明方式类似于函数,只是比函数多了一个delegate关键字 static class AsyncTest
{
public static void Method()
{
Console.WriteLine("AsyncTest类中的Method()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
{
Console.WriteLine("AsyncTest类中的Method()函数的线程托管于线程池");
}
else
{
Console.WriteLine("AsyncTest类中的Method()函数的线程没有托管在线程池上");
}
}
} class Program
{
static void Main(string[] args)
{
Console.WriteLine("Program类中的Main()函数的线程ID是{0}", Thread.CurrentThread.ManagedThreadId);//Environment.CurrentManagedThreadId
if (Thread.CurrentThread.IsThreadPoolThread)//判断当前线程是否托管在线程池上
{
Console.WriteLine("Program类中的Main()函数的线程托管于线程池");
}
else
{
Console.WriteLine("Program类中的Main()函数的线程没有托管在线程池上");
}
Console.WriteLine(); //把Method 方法分配给委托对象
AsyncHandler async = AsyncTest.Method; // //发起一个异步调用的方法,返回IAsyncResult 对象
IAsyncResult result = async.BeginInvoke(null,null); //这里会阻碍线程,直到方法执行完毕
async.EndInvoke(result); Console.Read();
}
}
运行结果如下图所示

从上图可以看出,委托的BeginInvoke,是新开了一个线程运行方法,并且该线程是托管在线程池上的
上面两个代码的示例,区别在于,第二个使用的是委托的BeginInvoke方法
通过反编译工具,我们可以查看委托具体做了什么,一般.net Reflector不错,最近新发现一个叫ILSpy的,开源的反编译工具
https://github.com/icsharpcode/ILSpy 官网的.sln是VS2013的,如果不想自己编译的,也可以去官网下载编译好的exe程序
官网链接是http://ilspy.net/ 右上角有一个Download Binaries,点击下载就可以了
接下来上图,反编译的结果

从上图可以发现AsyncHandler委托的结构如下
public extern AsyncHandler(object @object, IntPtr method);
public virtual extern IAsyncResult BeginInvoke(AsyncCallback callback, object @object);
public virtual extern void EndInvoke(IAsyncResult result);
public virtual extern void Invoke();
而且可以发现继承层次,AsyncHandler从MulticastDelegate继承,再上层是Delegate类(注意,大写的Delegate表示的是类,而不是delegate关键字)
去msdn上找了相关信息
Delegate类
public abstract class Delegate : ICloneable,ISerializable The Delegate class is the base class for delegate types.
Delegate类是delegate类型的基类
However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class.
然后只有系统和编译器才可以从Delegate类或者MulticastDelegate类进行派生
It is also not permissible to derive a new type from a delegate type.
从委托类型派生一个新类型也是不允许的
The Delegate class is not considered a delegate type; it is a class used to derive delegate types.
Delegate类并不是委托类型,它只是一个用于派生委托类型的类。
Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.
大多数语言实现了delegate关键字,并且这些语言的编译器是能够从MulticastDelegate类进行派生的,所以,开发者们应该使用开发语言所提供的delegate关键字 The common language runtime provides an Invoke method for each delegate type, with the same signature as the delegate.
CLR为每一个委托类型提供了一个Invoke方法,并且该方法和delegate拥有相同的签名 The common language runtime provides each delegate type with BeginInvoke and EndInvoke methods, to enable asynchronous invocation of the delegate.
CLR为每一个委托类型提供了BeginInvoke和EndInvoke方法,来确保委托的异步调用
MulticastDelegate类
public abstract class MulticastDelegate : Delegate MulticastDelegate is a special class. Compilers and other tools can derive from this class, but you cannot derive from it explicitly. The same is true of the Delegate class.
多播委托是一个特殊的类。编译器或者其他工具,可以从从这个类派生,但是你不能显式地从这个类派生。同样的规则适用于Delegate类。 In addition to the methods that delegate types inherit from MulticastDelegate, the common language runtime provides two special methods: BeginInvoke and EndInvoke.
delegate关键字声明的委托,不但继承了多播委托的方法,并且CLR还提供了两个特殊的方法BeginInvoke和EndInvoke
更多关于BeginInvoke和EndInvoke的信息可以参见此链接[使用异步方式调用同步方法]http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.110).aspx
使用委托进行异步编程http://msdn.microsoft.com/zh-cn/library/22t547yb(v=vs.110).aspx
转载自http://www.cnblogs.com/AndyHuang/archive/2008/12/24/1361267.html (有自己的修改和理解)
通过委托来实现异步 Delegate的BeginInvoke和EndInvoke的更多相关文章
- 用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
让我们首先了解下什么时候用到C#异步调用: .NET Framework 允许您C#异步调用任何方法.定义与您需要调用的方法具有相同签名的委托:公共语言运行库将自动为该委托定义具有适当签名的Begin ...
- 【C#】用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程
让我们首先了解下什么时候用到C#异步调用: .NET Framework 允许您C#异步调用任何方法.定义与您需要调用的方法具有相同签名的委托:公共语言运行库将自动为该委托定义具有适当签名的Begin ...
- delegate 中的BeginInvoke和EndInvoke方法
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能 ...
- 读书笔记 C#委托的BeginInvoke、EndInvoke之浅析
c#中有一种类型叫委托,它是一种引用类型.可以引用静态与非静态的方法,且这些方法的参数列表和返回值类型必须与所声明的委托一致. 委托引用的方法可以通过BeginInvoke和EndInvoke来异步进 ...
- C#--委托的同步,异步,回调函数
原文地址 同步调用 委托的Invoke方法用来进行同步调用.同步调用也可以叫阻塞调用,它将阻塞当前线程,然后执行调用,调用完毕后再继续向下进行. using System; using System. ...
- c#委托中的同步和异步方法即BeginInvoke和EndInvoke
学习多线程之前我们先了解一下电脑的一些概念,比如进程,线程,这个参考https://www.cnblogs.com/loverwangshan/p/10409755.html 这篇文章.今天我们接着来 ...
- C#线程系列讲座(1):BeginInvoke和EndInvoke方法
一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行.这就需要在同一个进程中开启多个 ...
- C#线程应用实例(part1) 之 BeginInvoke和EndInvoke
最近这个公司是做 winfrom 开发的 , 这段时间就好好的学学WCF , 公司框架什么的自己去琢磨! 这里主要写一些 winfrom 中 用到的一些陌生 技术 1.BeginInvoke 以前B ...
- 转:C#线程系列讲座(1) BeginInvoke和EndInvoke方法
转载自:http://www.cnblogs.com/levin9/articles/2319248.html 开发语言:C#3.0IDE:Visual Studio 2008本系列教程主要包括如下内 ...
随机推荐
- JS-【同页面多次调用】tab选项卡封装
这两天遇到一个页面,同一个页面中同一个特效会用好多次,比如tab,比如轮播等.我又不想很不负责任的复制一遍代码,那样页面臃肿,自己心里也堵得慌.于是就想着把代码封装起来多次调用. 对于封装,只在公开课 ...
- 用Iterator实现遍历集合
使用Collection类的Iterator,可以方便的遍历Vector, ArrayList, LinkedList等集合元素,避免通过get()方法遍历时,针对每一种对象单独进行编码. 示例: C ...
- sencha touch 入门系列 (九)sencha touch 视图组件简介
对于一个普通用户来说,你的项目就是一组简单的视图集合,用户直接通过跟视图进行交互来操作你的应用,对于一个开发人员来说,视图是一个项目的入口,虽然大部分时候最有价值的部分是在model层和control ...
- SPF难以解决邮件伪造的现状以及方案
邮件伪造的现状 仿冒域名 私搭邮服仿冒域名: 例如某公司企业的域名是example.com,那么攻击者可以搭建一个邮服,也把自己的域名配置为example.com,然后发邮件给真实的企业员工xxx@e ...
- 【BZOJ4423】[AMPPZ2013]Bytehattan 对偶图+并查集
[BZOJ4423][AMPPZ2013]Bytehattan Description 比特哈顿镇有n*n个格点,形成了一个网格图.一开始整张图是完整的.有k次操作,每次会删掉图中的一条边(u,v), ...
- LISTAGG
LISTAGG(measure_expr [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER query_partition_clause] S ...
- html 中 div 盒子并排展示
在项目中,遇到一个前端问题,觉得小问题就别去找前端工程师解决了,还是自己动动手吧. 相信不管小问题,大问题 都应该先自己尝试解决,google .度娘查查资料,这绝对是增加理解和记忆的好机会. ##问 ...
- mysql-blog
https://www.cnblogs.com/zhanht/p/5450559.html
- 第二次作业(WordCount)重制版
Github项目地址:https://gitee.com/DamonGetup/WordCount/tree/master 基本功能: 对程序设计语言源文件统计字符数.单词数.行数,统计结果以指定格式 ...
- Delphi中那些容易混淆的基础(@、^、Addr、Pointer,Move、CopyMemory,GetMem和FreeMem、GetMemory和FreeMemory、New和Dispose、StrAlloc和StrDispose、AllocMem)
@.^.Addr.Pointer Delphi(Pascal)中有几个特殊的符号,如@.^等,弄清楚这些符号的运行,首先要明白Delphi指针的一些基础知识:指针,是一个无符号整数(unsigned ...