一、概念
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.
一个使用IAsyncResult 设计模式进行的异步操作,通过实现了两个名为BeginOperationName 和EndOperationName 的方法,分别进行开始和结束异步操作OperationName ;
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.
例如,FileStream 类提供了BeginRead和EndRead两个方法来从一个文件中异步地读取字节。BeginRead和EndRead 这两个方法实现了Read方法的异步版本。
After calling BeginOperationName, an application can continue executing instructions on the calling thread while the asynchronous operation takes place on a different thread.
在调用了BeginOperationName[例如BeginRead]后,应用程序可以继续在调用线程上执行指令,同时异步操作将会在另外一个不同的线程执行
For each call to BeginOperationName, the application should also call EndOperationName to get the results of the operation.
每一次调用BeginOperationName[例如BeginRead],应用程序还应该调用EndOperationName [例如EndRead]来获取一步操作的结果。
二、Beginning an Asynchronous Operation开始异步操作
The BeginOperationName method begins asynchronous operation OperationName and returns an object that implements the IAsyncResult interface.
BeginOperationName 方法开始异步操作OperationName ,并且返回一个实现了IAsyncResult 接口的对象
IAsyncResult objects store information about an asynchronous operation.
IAsyncResult 对象存储了关于异步操作的信息。 [实现了IAsyncResult 的对象]
IAsyncResult 接口的属性 http://msdn.microsoft.com/en-us/library/System.IAsyncResult_properties(v=vs.110).aspx
Object AsyncState { get; } | An optional application-specific object that contains information about the asynchronous operation. |
WaitHandle AsyncWaitHandle { get; } | A WaitHandle that can be used to block application execution until the asynchronous operation completes. |
bool CompletedSynchronously { get; } | A value that indicates whether the asynchronous operation completed on the thread used to call BeginOperationName instead of completing on a separate ThreadPool thread. |
bool IsCompleted { get; } | value that indicates whether the asynchronous operation has completed. |
第一个属性,AsyncState:一个可选的应用程序对象,它包含了关于一步操作的信息
第二个属性,AsyncWaitHandle ,一个WaitHandle,可以用来阻塞应用程序的执行,直到异步操作完成
第三个属性,CompletedSynchronously , 指示异步操作 是完成于调用BeginOperationName 方法的线程 还是完成于线程池上独立的一个线程
第四个属性,IsCompleted ,指示异步操作是否完成
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.//BeginOperationName方法的签名不包含out类型的参数
The BeginOperationName method signature also includes two additional parameters.//BeginOperationName方法会包含两个额外的参数
The first of these defines an AsyncCallback delegate that references a method that is called when the asynchronous operation completes.//第一个参数是AsyncCallback委托引用的一个方法,会在异步操作完成的时候调用
The caller can specify null (Nothing in Visual Basic) if it does not want a method invoked when the operation completes.//如果在异步完成的时候不需要调用其他方法,AsyncCallback委托可以是null
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.
//如果BeginOperationName 方法有其他的操作参数,比如一个用于存放从文件中读取的字节的字节数组,那么AsyncCallback 委托和应用程序状态对象 会是BeginOperationName方法参数列表中最后的两个参数
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.
调用BeginOperationName 方法之后,会立即将返回到主线程,不会阻塞
If the BeginOperationName method throws exceptions, the exceptions are thrown before the asynchronous operation is started.
如果BeginOperationName 抛出异常的话,这些异常将会在异步操作开始之前抛出
If the BeginOperationName method throws exceptions, the callback method is not invoked.
如果BeginOperationName 抛出异常的话,那么callback 方法就不会被调用
三、Ending an Asynchronous Operation结束异步操作
The EndOperationName method ends asynchronous operation OperationName.//EndOperationName 方法用于结束异步操作OperationName[例如EndRead用于结束异步的Read]
The return value of the EndOperationName method is the same type returned by its synchronous counterpart and is specific to the asynchronous operation.
//EndOperationName 和同步副本拥有相同类型的返回值,针对于特定的异步操作
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.
//例如,EndRead 返回从filestream中读取的字节数;EndGetHostByName 返回一个IPHostEntry包含主机信息的IPHostEntry 对象
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.
//EndOperationName 方法带有任何out或ref类型的参数,参数声明在同步版本方法的签名中
In addition to the parameters from the synchronous method, the EndOperationName method also includes an IAsyncResult parameter.
//除了同步方法中的参数外,EndOperationName 还需要包括IAsyncResult 类型的参数
Callers must pass the instance returned by the corresponding call to BeginOperationName.
//调用EndOperationName 方法的时候,必须将BeginOperationName返回IAsyncResult对象传递给EndOperationName 中的IAsyncResult 参数
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.
//如果调用EndOperationName 方法的时候,返回IAsyncResult 对象的异步操作还没有完成的话,那么EndOperationName 将会阻塞调用它的线程,直到异步操作完成
Exceptions thrown by the asynchronous operation are thrown from the EndOperationName method.
异步操作抛出的一场,是从EndOperationName 方法抛出的
The effect of calling the EndOperationName method multiple times with the same IAsyncResult is not defined.
同一个IAsyncResult 对象多次调用EndOperationName 方法的效果是未知的
Likewise, calling the EndOperationName method with an IAsyncResult that was not returned by the related Begin method is also not defined.
如果通过一个不是Begin method 返回的IAsyncResult 对象,来调用EndOperationName 方法的效果,这也是未知的
For either of the undefined scenarios, implementers should consider throwing InvalidOperationException.
对于以上两种未定义的一场,调用者应该考虑抛出InvalidOperationException
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.
//实现IAsyncResult 设计模式的人,应该通知调用者异步操作已经完成,通过将IsCompleted 属性设置为true;并且调用callback 方法同时,向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.
//直接在应用程序的主线程中调用EndOperationName 方法来阻塞应用程序,直到异步操作完成
第二种方法
Use the AsyncWaitHandle to block application execution until one or more operations are complete.
使用AsyncWaitHandle 来阻塞应用程序的执行,直到一个或者多个操作完成
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.
//通过轮询的方式查看IsCompleted 属性来确认异步操作是否完成,如果完成的话,就调用EndOperationName 方法
Use an AsyncCallback delegate to specify a method to be invoked when the operation is complete.
//使用AsyncCallback 委托来标记一个方法,当异步操作完成的时候,就调用那个方法