Asynchronous method invocation: Difference between revisions

Content deleted Content added
FrescoBot (talk | contribs)
Citation bot (talk | contribs)
m Add: isbn, website. Removed parameters. You can use this bot yourself. Report bugs here. | Headbomb
Line 2:
 
== Background ==
'''AMI''' is a [[software design pattern|design pattern]] for [[asynchrony (computer programming)|asynchronous]] invocation of potentially long-running [[method (computer science)|methods]] of an [[object (computer science)|object]].<ref name="Async.34.2#71139">{{cite web|url=http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.2.html#71139 |title=Asynchronous Method Invocation |accessdate=22 November 2008 |workwebsite=Distributed Programming with Ice |publisher=ZeroC, Inc. |deadurl=yes |archiveurl=https://web.archive.org/web/20080105093534/http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.2.html |archivedate=5 January 2008 |df= }}</ref>
It is equivalent to the [[IOU]] pattern described in 1996 by Allan Vermeulen.<ref>{{cite journal |last=Vermeulen |first=Allan |date=June 1996 |title=An Asynchronous Design Pattern |journal=[[Dr. Dobb's Journal]] |url=http://www.ddj.com/184409898 |accessdate=22 November 2008 }}</ref><ref>{{cite book |last=Nash |first=Trey |title=Accelerated C# 2008 | year=2007 |publisher=Apress |isbn=978-1-59059-873-3 |chapter=Threading in C# }}</ref>
 
Line 24:
====Example====
 
The following example is loosely based on a standard AMI style used in the [[.NET Framework]].<ref name="ms228969">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228969.aspx |title=Asynchronous Programming Design Patterns |accessdate=22 November 2008 |workwebsite=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= https://web.archive.org/web/20081122091746/http://msdn.microsoft.com/en-us/library/ms228969.aspx| archivedate= 22 November 2008 <!--DASHBot-->| deadurl= no}}</ref>
Given a method <code>Accomplish</code>, one adds two new methods <code>BeginAccomplish</code> and <code>EndAccomplish</code>:
 
Line 36:
}</source>
 
Upon calling <code>BeginAccomplish</code>, the client immediately receives an object of type <code>AsyncResult</code> (which implements the <code>IAsyncResult</code> 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 <code>EndAccomplish</code> (passing the previously received object), which blocks until the method has completed and the result is available.<ref name="ms228963">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228963.aspx |title=Asynchronous Programming Overview |accessdate= 22 November 2008 |workwebsite=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= https://web.archive.org/web/20081207092841/http://msdn.microsoft.com/en-us/library/ms228963.aspx| archivedate= 7 December 2008 <!--DASHBot-->| deadurl= no}}</ref> The <code>AsyncResult</code> object normally provides at least a method that allows the client to query whether the long-running method has already completed:
 
<source lang="csharp">
Line 45:
}</source>
 
One can also pass a callback method to <code>BeginAccomplish</code>, to be invoked when the long-running method completes. It typically calls <code>EndAccomplish</code> 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.<ref name="ms228972">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228972.aspx |title=Using an AsyncCallback Delegate to End an Asynchronous Operation |accessdate= 22 November 2008 |workwebsite=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= https://web.archive.org/web/20081223205326/http://msdn.microsoft.com/en-us/library/ms228972.aspx| archivedate= 23 December 2008 <!--DASHBot-->| deadurl= no}}</ref><ref name="Async.34.3#76161">{{cite web|url=http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.3.html#76161 |title=Concurrency Issues |accessdate=22 November 2008 |workwebsite=Distributed Programming with Ice |publisher=ZeroC, Inc. |deadurl=yes |archiveurl=https://web.archive.org/web/20080328070322/http://www.zeroc.com/doc/Ice-3.2.1/manual/Async.34.3.html |archivedate=28 March 2008 |df= }}</ref>
 
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 <code>AccomplishAsync</code> instead of <code>BeginAccomplish</code>.<ref name=nageletal>{{cite book|title=Professional C# 2008|author1=Christian Nagel |author2=Bill Evjen |author3=Jay Glynn |author4=Karli Watson |author5=Morgan Skinner |last-author-amp=yes |pages=570&ndash;571|publisher=Wiley|year=2008|isbn13isbn=9780470191378|chapter=Event-based Asynchronous Pattern}}</ref><ref name="hkasytyf">{{cite web |url=http://msdn.microsoft.com/en-us/library/hkasytyf.aspx |title=Multithreaded Programming with the Event-based Asynchronous Pattern |accessdate= 22 November 2008 |workwebsite=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= https://web.archive.org/web/20081225175311/http://msdn.microsoft.com/en-us/library/hkasytyf.aspx| archivedate= 25 December 2008 <!--DASHBot-->| deadurl= no}}</ref>
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 <code>AccomplishCompleted</code>) in the same thread in which <code>BeginAccomplish</code> 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.<ref name="ms228966">{{cite web |url=http://msdn.microsoft.com/en-us/library/ms228966.aspx |title=Deciding When to Implement the Event-based Asynchronous Pattern |accessdate= 22 November 2008 |workwebsite=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= https://web.archive.org/web/20081122092048/http://msdn.microsoft.com/en-us/library/ms228966.aspx| archivedate= 22 November 2008 <!--DASHBot-->| deadurl= no}}</ref>
 
==References==
Line 54:
 
== Further reading ==
* {{cite book|title=Programming WPF|author1=Chris Sells |author2=Ian Griffiths |lastauthoramp=yes |chapter=Appendix C.3: The Event-Based Asynchronous Pattern|pages=747&ndash;749|publisher=O'Reilly|year=2007|isbn13isbn=9780596510374}}
* [http://articles.techrepublic.com.com/5100-10878_11-1044325.html Using asynchronous method calls in C#]