I am moving lots of code loosely based on asynchronous method invocation. How is it typically implemented (preferably in production)?
- How does it work?
- How is it used?
- How is it 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
. 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). The EndXXX
will return the method result (if not a void method) or throw an exception (if the method threw an exception).Have a look at the Stream class or on any delegate, they provide you with examples of those signatures. Also, there is a complete description of async calls on MSDN.
The pattern is usually implemented via .NET's IOCompletion port. 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.
The details (and the other approaches) vary widely depending on the resource in question.