.Net的异步机制(委托Delegate) - STEP 1
在阅读下面知识前,我已经认为你已经具有c#的基础,包括简单的委托知识; 代码使用VS2008开发,但是会在.Net Framework 2.0(C Sharp)编写
什么是.Net异步机制呢?
在解释这个话题前,我们先看看同步的程序,就是我们常用的Hello World 程序。
Code 1:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
图1
我们可以从图1看出,我们平常写的Hello 程序是同一个线程的,而且不是线程池理的线程程序。按照上面的程序稍做改动, 那么开始我们第一个异步的Hello World 程序。
使用.Net 的委托机制来为我们的程序提供异步操作行为。
1步, 为我们的AsyncTest(Hello方法) 声明一个委托
public delegate string AsyncEventHandler(string name);
2步,使用委托提供的BeginInvoke, EndInvoke 方法(具体使用下一篇文章详细介绍)来提供异步的调用。
string val = test.Hello("Andy Huang");
修改为
AsyncEventHandler async = test.Hello;
IAsyncResult result = async.BeginInvoke("Andy Huang", null, null);
string val = async.EndInvoke(result);
下面是完整的代码.
Code 2:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
注释1: 用=操作符在于分配委托对象时候不需要初始化;并且异步调用时候只能有一个目标方法.
图2
对比图1 和图2, ,可以看出(2,Thread ID:#10,Is PoolThread?True),在使用异步机制的时候,其实就是开启一个新的线程来执行我们的方法,并且这个线程来自 线程堆. 到这里,我们就很好的解释了”什么是.Net异步机制?”
说到这里,结束了吗? 可能大家会想其实使用异步就是多了个委托( public delegate string AsyncEventHandler(string name);) 那么到底为我们做了些什么呢?
通过反编译(微软提供的IL Disassembler)我们看到
图3
编译器为我们生成了下面类定义(其实委托就是一个类)
Code 3
public sealed class AsyncEventHandler : MulticastDelegate
{
public AsyncEventHandler(object @object, IntPtr method)
{....}
public virtual IAsyncResult BeginInvoke(string name, AsyncCallback callback, object @object)
{...}
public virtual string EndInvoke(IAsyncResult result)
{...}
public virtual string Invoke(string name)
{...}
}
继承于MulticastDelegate, 提供了BeginInvoke / EndInvoke / Invoke.
Invoke 是我们Code 1 同步调用的方法,当然我们也可以直接使用Invoke来同步调用.
BeginInvoke / EndInvoke 是Code 2中使用的异步调用的方法,下篇文章我会详细介绍
什么时候使用.Net异步机制呢?
异步操作通常用于执行完成时间可能较长的任务,如打开大文件、连接远程计算机或查询数据库。异步操作在主应用程序线程以外的线程中执行。应用程序调用方法异步执行某个操作时,应用程序仍然可以继续执行当前的程序。
.NET Framework 的许多方面都支持异步编程功能,这些方面包括:
· 文件(File) IO、流(Stream) IO、套接字(Socket) IO。
· 网络。
· 远程处理信道(HTTP、TCP)和代理。
· 使用 ASP.NET 创建的 XML Web services。
· ASP.NET Web 窗体。
· 使用 MessageQueue 类的消息队列。
以上有word 文档直接粘贴,排版可能不太看,你可以通过下面来下载相应的代码/文档
1, 代码
引用自 :http://www.cnblogs.com/30ErLi/archive/2010/09/19/1830726.html