Asynchronous Method Call

What Does Asynchronous Method Call Mean?

An asynchronous method call is a method used in .NET programming that returns to the caller immediately before the completion of its processing and without blocking the calling thread.

Advertisements

When an application calls an asynchronous method, it can simultaneously execute along with the execution of the asynchronous method that performs its task. An asynchronous method runs in a thread separate from the main application thread. The processing results are fetched through another call on another thread.

Asynchronous methods help optimize the execution of resources resulting in scalable application. These are used to execute time-consuming tasks such as opening large files, connecting to remote computers, querying a database, calling Web services and ASP.NET Web forms.

Asynchronous method call may also be referred to as asynchronous method invocation (AMI).

Techopedia Explains Asynchronous Method Call

Asynchronous method differs from synchronous method in the manner in which it returns from the call. While an asynchronous method call returns immediately, allowing the calling program to perform other operations, synchronous method calls wait for the method to complete before continuing with program flow.

The .NET framework has inbuilt asynchronous infrastructure so that any method can be asynchronously invoked without altering its code.

.NET framework provides two design patterns to implement the asynchronous method, which are those using asynchronous delegates (IASyncResult objects) and events. Asynchronous delegates’ pattern is more complex and provides flexibility, which makes it well-suited to various complex programming models. The event-based model is simple and should be used in most cases.

In the asynchronous delegates pattern, a delegate object uses two methods: BeginInvoke and EndInvoke. BeginInvoke has a list of parameters, which are similar to its wrapped function, along with two additional optional parameters; it returns the IAsyncResult object. EndInvoke returns two parameters (out and ref type) along with the IAsyncResult object. BeginInvoke is used for initiating the asynchronous call, whereas EndInvoke is used to retrieve the results of the asynchronous call.

Events-based asynchronous patterns use a class that has one or more methods, named MethodNameAsync, which have corresponding synchronous versions that execute on the current thread. Events-based patterns may also have a MethodNameCompleted event and MethodNameAsyncCancel method. This pattern enables the class to communicate with pending asynchronous operations using the delegate event model.

The following are a few tips related to asynchronous methods:

  • For high concurrency, asynchronous methods have to be avoided
  • Care needs to be taken while passing shared object references
  • EndXXX (called at the end of an asynchronous operation) has to be called to rethrow exceptions and avoid failure
  • By catching and saving all exception objects in asynchronous method, it can be rethrown during the EndXXX call
  • Controls in the user interface that initiate long-running asynchronous operations have to be disabled if they are only needed for that purpose
  • Asynchronous methods have to be implemented with an understanding of multithreading and where they prove to be more efficient than using synchronous methods.
Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…