[英]How Asynchronous method invocation pattern in c# works and implemented?
I am moving lots of code loosely based on asynchronous method invocation . 我正在基于异步方法调用松散地移动许多代码。 How is it typically implemented (preferably in production)? 通常如何实施(最好在生产中)?
The pattern is usually as follows: 该模式通常如下:
BeginXXX
method which receives all in and ref
arguments plus a AsyncCallback
delegate (may be null) and a state object reference (may also be null) and returns an IAsyncResult
. 您有一个BeginXXX
方法,该方法接收所有in和ref
参数,以及AsyncCallback
委托(可以为null)和状态对象引用(也可以为null),并返回IAsyncResult
。 This method is invoked to start the call, and it returns more or less immediately. 调用此方法开始调用,并立即或多或少返回。 EndXXX
method which takes the IAsyncResult
returned by the BeginXXX
operation plus any ref
and out
arguments which is invoked when the call is to be completed (either after callback, waiting on event in the IAsyncResult
or by blocking). 然后,您将拥有一个EndXXX
方法,该IAsyncResult
采用BeginXXX
操作返回的IAsyncResult
以及将在调用完成时调用的任何ref
和out
参数(在回调之后, 在IAsyncResult
等待事件或通过阻塞)。 The EndXXX
will return the method result (if not a void method) or throw an exception (if the method threw an exception). EndXXX
将返回方法结果(如果不是void方法)或引发异常(如果方法引发异常)。 Have a look at the Stream class or on any delegate, they provide you with examples of those signatures. 看一下Stream类或任何委托,它们为您提供了这些签名的示例。 Also, there is a complete description of async calls on MSDN . 另外, 在MSDN上还有异步调用的完整描述 。
The pattern is usually implemented via .NET's IOCompletion port . 该模式通常是通过.NET的IOCompletion端口实现的 。 This allows a small pool of threads to service many IO operations completing, with the BeginX
calling the appropriate Win32 API to initiate an asynchronous operation, the implementation of IAsyncResult holding any state (associated with the underlying operation via the OVERLAPPED
instance), and EndX
picking up the result. 这允许一小部分线程池为完成的许多IO操作提供服务,其中BeginX
调用适当的Win32 API来启动异步操作,IAsyncResult的实现保留任何状态(通过OVERLAPPED
实例与基础操作关联),以及EndX
选择结果。
The details (and the other approaches) vary widely depending on the resource in question. 细节(和其他方法)根据所涉及的资源而有很大差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.