https://msdn.microsoft.com/zh-cn/library/ms228963(v=vs.110).aspx

一、概念

An asynchronous operation that uses the IAsyncResult design pattern is implemented as two methods named BeginOperationName and EndOperationName that begin and end the asynchronous operation OperationName respectively.

For example, the FileStream class provides the BeginRead and EndRead methods to asynchronously read bytes from a file.

These methods implement the asynchronous version of the Read method.

After calling BeginOperationName, an application can continue executing instructions on the calling thread while the asynchronous operation takes place on a different thread.

For each call to BeginOperationName, the application should also call EndOperationName to get the results of the operation.

二、Beginning an Asynchronous Operation开始异步操作

The BeginOperationName method begins asynchronous operation OperationName and returns an object that implements the IAsyncResult interface.

IAsyncResult objects store information about an asynchronous operation.

The following table shows information about an asynchronous operation.

第一个属性,AsyncState,An optional application-specific object that contains information about the asynchronous operation.

第二个属性,AsyncWaitHandle ,A WaitHandle that can be used to block application execution until the asynchronous operation completes.

第三个属性,CompletedSynchronously , A value that indicates whether the asynchronous operation completed on the thread used to call BeginOperationName instead of completing on a separateThreadPool thread.

第四个属性,IsCompleted ,A value that indicates whether the asynchronous operation has completed.

A BeginOperationName method takes any parameters declared in the signature of the synchronous version of the method that are passed by value or by reference.

Any out parameters are not part of the BeginOperationName method signature.

The BeginOperationName method signature also includes two additional parameters.

The first of these defines an AsyncCallback delegate that references a method that is called when the asynchronous operation completes.

The caller can specify null (Nothing in Visual Basic) if it does not want a method invoked when the operation completes.

The second additional parameter is a user-defined object.

This object can be used to pass application-specific state information to the method invoked when the asynchronous operation completes.

If a BeginOperationName method takes additional operation-specific parameters, such as a byte array to store bytes read from a file, the AsyncCallback and application state object are the last parameters in the BeginOperationName method signature.

 public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject);
public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback requestCallback, object stateObject);

Begin OperationName returns control to the calling thread immediately.

If the BeginOperationName method throws exceptions, the exceptions are thrown before the asynchronous operation is started.

If the BeginOperationName method throws exceptions, the callback method is not invoked.

三、Ending an Asynchronous Operation结束异步操作

The EndOperationName method ends asynchronous operation OperationName.

The return value of the EndOperationName method is the same type returned by its synchronous counterpart and is specific to the asynchronous operation.

For example, the EndRead method returns the number of bytes read from a FileStream and the EndGetHostByName method returns an IPHostEntry object that contains information about a host computer.

 public class FileStream : Stream
{
public override int Read(byte[] array, int offset, int count);
public override int EndRead(IAsyncResult asyncResult);
}
public static class Dns
{
public static IPHostEntry GetHostByName(string hostName);
public static IPHostEntry EndGetHostByName(IAsyncResult asyncResult);
}

The EndOperationName method takes any out or ref parameters declared in the signature of the synchronous version of the method.

In addition to the parameters from the synchronous method, the EndOperationName method also includes an IAsyncResult parameter.

Callers must pass the instance returned by the corresponding call to BeginOperationName.

If the asynchronous operation represented by the IAsyncResult object has not completed when EndOperationName is called, EndOperationName blocks the calling thread until the asynchronous operation is complete.

Exceptions thrown by the asynchronous operation are thrown from the EndOperationName method.

The effect of calling the EndOperationName method multiple times with the same IAsyncResult is not defined.

Likewise, calling the EndOperationName method with an IAsyncResult that was not returned by the related Begin method is also not defined.

Note:

For either of the undefined scenarios, implementers should consider throwing InvalidOperationException.

Note:

Implementers of this design pattern should notify the caller that the asynchronous operation completed by setting IsCompleted to true, calling the asynchronous callback method (if one was specified) and signaling the AsyncWaitHandle.

Application developers have several design choices for accessing the results of the asynchronous operation.

The correct choice depends on whether the application has instructions that can execute while the operation completes.

If an application cannot perform any additional work until it receives the results of the asynchronous operation, the application must block until the results are available.

To block until an asynchronous operation completes, you can use one of the following approaches:

第一种方法

Call EndOperationName from the application’s main thread, blocking application execution until the operation is complete.

第二种方法

Use the AsyncWaitHandle to block application execution until one or more operations are complete.

Applications that do not need to block while the asynchronous operation completes can use one of the following approaches:

Poll for operation completion status by checking the IsCompleted property periodically and calling EndOperationName when the operation is complete.

Use an AsyncCallback delegate to specify a method to be invoked when the operation is complete.

==总结==2015年10月08日更新==

很早之前就在纸上写了,一直懒,现在才更新

1.BeginRead和EndRead,是Read的异步版本,调用BeginRead之后,程序还会继续往下执行,异步操作是在一个不同的线程上处理的。

每一次调用BeginRead,都要调用一次EndRead来获取操作结果。

2.BeginRead返回的对象实现了IAsyncResult接口

BeginRead的参数,除了和同步版本一样的参数外(不附带out参数),还有2个额外的参数

多出来的参数1:AsyncCallback委托,指定了在异步操作结束后,需要调用的方法

多出来的参数2:用户自定义的一个Object,可以用来传递应用程序的状态信息

这2个多出来的参数放在BeginRead的参数列表的最后

异常处理

如果BeginRead触发异常的话,那么异常会在异步操作开始之前被抛出,并且回调方法不会触发

3.EndRead的返回值的类型,和同步方法的返回值的类型是一样的

EndRead的参数,会附带同步方法中的out以及ref参数

调用者需要把BeginRead的返回对象(实现了IAsyncResult接口的对象),作为EndRead的参数

如果EndRead方法在调用的时候,IAsyncResult对象的所代表的对象的操作尚未完成,那么调用EndRead的线程就会一直阻塞,直到异步操作完成

异常处理

异步操作导致的异常,会在EndRead操作抛出

有2种方法来处理操作结果(不同的情况处理方式不同)

1.如果应用程序的下一步操作依赖于返回结果

(1)主线程调用EndRead方法,在异步操作完成之前阻塞主线程

(2)使用AsyncWaitHandle阻塞主线程

2.如果应用程序的下一步操作不依赖返回结果

(1)轮询IsCompleted属性,等到完成时再调用EndRead

(2)使用AsyncCallback指定回调方法

Asynchronous Programming Model (APM)异步编程模型的更多相关文章

  1. 【专栏学习】APM——异步编程模型(.NET不推荐)

    (1)learning hard C#学习笔记 异步1:<learning hard C#学习笔记>读书笔记(20)异步编程 (2)<C# 4.0 图解教程> 22.4 异步编 ...

  2. C#异步编程の-------异步编程模型(APM)

    术语解释: APM               异步编程模型, Asynchronous Programming Model EAP                基于事件的异步编程模式, Event ...

  3. 【温故知新】c#异步编程模型(APM)--使用委托进行异步编程

    当我们用到C#类许多耗时的函数XXX时,总会存在同名的类似BeginXXX,EndXXX这样的函数. 例如Stream抽象类的Read函数就有 public abstract int Read(byt ...

  4. C#异步编程模型

    什么是异步编程模型 异步编程模型(Asynchronous Programming Model,简称APM)是C#1.1支持的一种实现异步操作的编程模型,虽然已经比较“古老”了,但是依然可以学习一下的 ...

  5. .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)

    本文内容 异步编程类型 异步编程模型(APM) 参考资料 首先澄清,异步编程模式(Asynchronous Programming Patterns)与异步编程模型(Asynchronous Prog ...

  6. 转:[你必须知道的异步编程]——异步编程模型(APM)

    本专题概要: 引言 你知道APM吗? 你想知道如何使用异步编程模型编写代码吗? 使用委托也可以实现异步编程,你知道否? 小结 一.引言 在前面的C#基础知识系列中介绍了从C#1.0——C#4.0中一些 ...

  7. [你必须知道的异步编程]——异步编程模型(APM)

    本专题概要: 引言 你知道APM吗? 你想知道如何使用异步编程模型编写代码吗? 使用委托也可以实现异步编程,你知道否? 小结 一.引言 在前面的C#基础知识系列中 介绍了从C#1.0——C#4.0中一 ...

  8. 一、异步编程模型(APM)

    一.概念 APM即异步编程模式的简写(Asynchronous Programming Model).大家在写代码的时候或者查看.NET 的类库的时候肯定会经常看到和使用以BeginXXX和EndXX ...

  9. c# 基于委托的异步编程模型(APM)测试用例

    很多时候,我们需要程序在执行某个操作完成时,我们能够知道,以便进行下一步操作. 但是在使用原生线程或者线程池进行异步编程,没有一个内建的机制让你知道操作什么时候完成,为了克服这些限制,基于委托的异步编 ...

随机推荐

  1. Android Processes and Threads

    Processes and Threads When an application component starts and the application does not have any oth ...

  2. you-get 下载网络上的富媒体信息

    You-Get 乃一小小哒命令行程序,提供便利的方式,下载网络上的富媒体信息. 利用you-get下载这个网页的视频: $ you-get http://www.fsf.org/blogs/rms/2 ...

  3. Microsoft Web Application Stress Tool 使用

    为了测试数据的准备性,首先需要删除缓存和Cookies等临时文件.启动IE后打开“工具”菜单下的“Internet”选项命令,在打开的“Internet选项”窗口的“常规”选项卡中,单击“Intern ...

  4. 神兽保佑-代码无BUG

    ┏┓ ┏┓┏┛┻━━━┛┻┓┃ ┃ ┃ ━ ┃┃ ┳┛ ┗┳ ┃┃ ┃┃ ┻ ┃┃ ┃┗━┓ ┏━┛ ┃ ┃   神兽保佑 ┃ ┃   代码无BUG!       ┃ ┗━━━┓       ┃ ┣┓ ...

  5. WCF(五) 深入理解绑定

    适用于本机WCF-WCF交互性能最佳的绑定: 允许跨主机,但只能用于部署同一台主机上,不能访问命名管道 netNamePipeBinding总结 一 WCF与SOA SOA是一种通过为所有软件提供服务 ...

  6. mysql如何按周统计数据?

    转自:https://www.cnblogs.com/wanghetao/p/3920124.html MySql 按周/月/日统计数据的方法 知识关键词:DATE_FORMAT  select DA ...

  7. etc/fstab

    etc/fstab 就是在开机引导的时候自动挂载到linux的文件系统 设备名称 挂载点 分区的类型 挂载选项 dump选项 fsck选项UUID=ce25cdc7-434f-420b-b3 / ex ...

  8. __FILE__ 与 $_SERVER['SCRIPT_FILENAME']的区别

    二者都表明了本文件的绝对路径,区别在于,$_SERVER['SCRIPT_FILENAME']指向当前执行脚本的绝对路径:__FILE__指向当前文件的绝对路径:也就是写在哪个文件里就是哪里. 例子: ...

  9. python基础之函数式编程、匿名函数、内置函数

    一 函数式编程 不修改外部状态. 模仿数学里得函数进行编程. 用函数编程写出得代码相当精简. 可读性比较差. 例子: y=2*x+1 x=1 def test(x): return 2*x+1 tes ...

  10. Python开发【Django】:组合搜索、JSONP、XSS过滤

    组合搜索 做博客后台时,需要根据文章的类型做不同的检索 1.简单实现 关联文件: from django.conf.urls import url from . import views urlpat ...