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?
0


source to share


2 answers


The template usually looks like this:

  • You have a method BeginXXX

    that takes all arguments in and ref

    plus AsyncCallback

    delegate (can be null) and a state object reference (can also be null) and returns IAsyncResult

    . This method is called to start the call, and it returns more or less immediately.
  • Then you have a method EndXXX

    that takes the IAsyncResult

    return of the operation BeginXXX

    , plus any arguments ref

    and out

    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 .

+4


source


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.

0


source







All Articles