Asynchronous method invocation

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In multithreaded computer programming, asynchronous method invocation (AMI), also known as asynchronous method calls or the asynchronous pattern, is a design pattern in which the call site is not blocked while waiting for the called code to finish. Instead, the calling thread is notified when the reply arrives. Polling for a reply is an undesired option.

In multithreaded computer programming, asynchronous method invocation (AMI), also known as asynchronous method calls or the asynchronous pattern, is a design pattern in which the call site is not blocked while waiting for the called code to finish. Instead, the calling thread is notified when the reply arrives. Polling for a reply is an undesired option. ====Example==== The following example is loosely based on a standard AMI style used in the .NET Framework. Given a method Accomplish , one adds two new methods BeginAccomplish and EndAccomplish : using System; class Example { Result Accomplish(string[] args); IAsyncResult BeginAccomplish(string[] args); Result EndAccomplish(IAsyncResult a); } Upon calling BeginAccomplish , the client immediately receives an object of type AsyncResult (which implements the System.IAsyncResult interface), so it can continue the calling thread with unrelated work. In the simplest case, eventually there is no more such work, and the client calls EndAccomplish (passing the previously received object), which blocks until the method has completed and the result is available. The AsyncResult object normally provides at least a method that allows the client to query whether the long-running method has already completed: namespace System; public interface IAsyncResult { bool IsCompleted(); // ... } One can also pass a callback method to BeginAccomplish , to be invoked when the long-running method completes. It typically calls EndAccomplish to obtain the return value of the long-running method. A problem with the callback mechanism is that the callback function is naturally executed in the worker thread (rather than in the original calling thread), which may cause race conditions. In the .NET Framework documentation, the term event-based asynchronous pattern refers to an alternative API style (available since .NET 2.0) using a method named AccomplishAsync instead of BeginAccomplish . A superficial difference is that in this style the return value of the long-running method is passed directly to the callback method. Much more importantly, the API uses a special mechanism to run the callback method (which resides in an event object of type AccomplishCompleted ) in the same thread in which BeginAccomplish was called. This eliminates the danger of race conditions, making the API easier to use and suitable for software components; on the other hand this implementation of the pattern comes with additional object creation and synchronization overhead. ==References==

Text derived from https://en.wikipedia.org/w/index.php?oldid=1369529340 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001369529340 · captured 1786798849.0 · version history

Cite this page as: encyclopegia.org, “Asynchronous method invocation”, snapshot enwiki-001369529340. https://encyclopegia.org/a/asynchronous-method-invocation