异步编程模型 (APM)Asynchronous Programming Model (APM)

使用 IAsyncResult 设计模式的异步操作是通过名为 BeginOperationNameEndOperationName 的两个方法来实现的,这两个方法分别开始和结束异步操作 OperationName 。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. 例如, FileStream 类提供 BeginReadEndRead 方法来从文件异步读取字节。For example, the FileStream class provides the BeginRead and EndRead methods to asynchronously read bytes from a file. 这两个方法实现了 Read 方法的异步版本。These methods implement the asynchronous version of the Read method.

备注

从 .NET Framework 4 开始,任务并行库为异步和并行编程提供了一种新模型。Starting with the .NET Framework 4, the Task Parallel Library provides a new model for asynchronous and parallel programming. 有关详细信息,请参阅 Task Parallel Library (TPL)Task-based Asynchronous Pattern (TAP)For more information, see Task Parallel Library (TPL) and Task-based Asynchronous Pattern (TAP)).

在调用 BeginOperationName 后,应用程序可以继续在调用线程上执行指令,同时异步操作在另一个线程上执行。After calling BeginOperationName, an application can continue executing instructions on the calling thread while the asynchronous operation takes place on a different thread. 每次调用 BeginOperationName 时,应用程序还应调用 EndOperationName 来获取操作的结果。For each call to BeginOperationName, the application should also call EndOperationName to get the results of the operation.

开始异步操作Beginning an Asynchronous Operation

BeginOperationName 方法开始异步操作 OperationName ,并返回实现 IAsyncResult 接口的对象。The BeginOperationName method begins asynchronous operation OperationName and returns an object that implements the IAsyncResult interface. IAsyncResult 对象存储有关异步操作的信息。IAsyncResult objects store information about an asynchronous operation. 下表显示有关异步操作的信息。The following table shows information about an asynchronous operation.

成员Member 说明Description
AsyncState 一个特定于应用程序的可选对象,其中包含有关异步操作的信息。An optional application-specific object that contains information about the asynchronous operation.
AsyncWaitHandle 一个 WaitHandle ,可用来在异步操作完成之前阻止应用程序执行。A WaitHandle that can be used to block application execution until the asynchronous operation completes.
CompletedSynchronously 一个值,指示异步操作是否是在用于调用 BeginOperationName 的线程上完成,而不是在单独的 ThreadPool 线程上完成。A value that indicates whether the asynchronous operation completed on the thread used to call BeginOperationName instead of completing on a separate ThreadPool thread.
IsCompleted 一个值,指示异步操作是否已完成。A value that indicates whether the asynchronous operation has completed.

BeginOperationName 方法采用该方法的同步版本的签名中声明的任何参数(由值传递或由引用传递)。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. BeginOperationName 方法签名中不包含任何输出参数。Any out parameters are not part of the BeginOperationName method signature. BeginOperationName 方法签名另外还包括两个其他参数。The BeginOperationName method signature also includes two additional parameters. 第一个参数定义一个 AsyncCallback 委托,此委托引用在异步操作完成时调用的方法。The first of these defines an AsyncCallback delegate that references a method that is called when the asynchronous operation completes. 如果调用方不希望在操作完成后调用方法,它可以指定 null (在 Visual Basic 中为Nothing )。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. 如果 BeginOperationName 方法还采用其他一些操作特定的参数(例如,一个用于存储从文件读取的字节的字节数组),则 AsyncCallback 和应用程序状态对象将是 BeginOperationName 方法签名中的最后两个参数。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 立即返回对调用线程的控制。BeginOperationName 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.

结束异步操作Ending an Asynchronous Operation

EndOperationName 方法用于结束异步操作 OperationName 。The EndOperationName method ends asynchronous operation OperationName. EndOperationName 方法的返回值与其同步对应方法的返回值类型相同,并且是特定于异步操作的。The return value of the EndOperationName method is the same type returned by its synchronous counterpart and is specific to the asynchronous operation. 例如, EndRead 方法返回从 FileStream 读取的字节数, EndGetHostByName 方法返回包含有关主机的信息的 IPHostEntry 对象。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. EndOperationName 方法采用该方法同步版本的签名中声明的所有输出参数或引用参数。The EndOperationName method takes any out or ref parameters declared in the signature of the synchronous version of the method. 除了来自同步方法的参数外,EndOperationName 方法还包括 IAsyncResult 参数。In addition to the parameters from the synchronous method, the EndOperationName method also includes an IAsyncResult parameter. 调用方必须将对应调用返回的实例传递给 BeginOperationNameCallers must pass the instance returned by the corresponding call to BeginOperationName.

如果调用 EndOperationNameIAsyncResult 对象表示的异步操作尚未完成,则 EndOperationName 将在异步操作完成之前阻止调用线程。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 方法引发的。Exceptions thrown by the asynchronous operation are thrown from the EndOperationName method. 未定义多次使用同一 IAsyncResult 调用 EndOperationName 方法的效果。The effect of calling the EndOperationName method multiple times with the same IAsyncResult is not defined. 同样,也未定义使用 IAsyncResult(相关 Begin 方法未返回)调用 EndOperationName 方法的效果。Likewise, calling the EndOperationName method with an IAsyncResult that was not returned by the related Begin method is also not defined.

备注

对于这两种未定义的情况,实施者应考虑引发 InvalidOperationExceptionFor either of the undefined scenarios, implementers should consider throwing InvalidOperationException.

备注

此设计模式的实施者应通知调用方异步操作已通过以下步骤完成:将 IsCompleted 设置为 true,调用异步回调方法(如果已指定一个回调方法),然后发送 AsyncWaitHandle信号。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:

在异步操作完成时不需要阻止的应用程序可使用下列方法之一:Applications that do not need to block while the asynchronous operation completes can use one of the following approaches:

请参阅See also