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.
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.