How does the asynchronous method call pattern work and is implemented in C #?
I am moving a lot of code freely based on an asynchronous method call . How is this usually done (preferably in production)?
- How it works?
- How is it used?
- How is this implemented?
The template usually looks like this:
- You have a method
BeginXXX
that takes all arguments in andref
plusAsyncCallback
delegate (can be null) and a state object reference (can also be null) and returnsIAsyncResult
. This method is called to start the call, and it returns more or less immediately. - Then you have a method
EndXXX
that takes theIAsyncResult
return of the operationBeginXXX
, plus any argumentsref
andout
that are called when the call should complete (either after a callback, waiting for an event in,IAsyncResult
or by blocking).EndXXX
will return the result of the method (if not the void method) or throw an exception (if the method threw an exception).
Take a look at the Stream class or any delegate, they will provide you with examples of these signatures. In addition, there is a complete description of asynchronous calls on MSDN .
source to share
The pattern is usually implemented via the .NET IOCompletion port . This allows a small pool of threads to serve a lot of I / O while BeginX
calling the appropriate Win32 API to initiate an asynchronous operation, an IAsyncResult implementation that executes any state (associated with the underlying operation through the OVERLAPPED
instance) and EndX
picks up the result.
The details (and other approaches) vary greatly depending on the resource in question.
source to share