Content deleted Content added
m Fix CS1 deprecated coauthor parameter errors using AWB |
m →Example: <source lang="csharp"> |
||
Line 15:
Given a method <code>Accomplish</code>, one adds two new methods <code>BeginAccomplish</code> and <code>EndAccomplish</code>:
<source lang="csharp">
Class Example
{
Line 22:
Result EndAccomplish(IAsyncResult a)
…
}</
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 |work=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= http://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">
Interface IAsyncResult
{
bool HasCompleted()
…
}</
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 |work=.NET Framework Developer's Guide |publisher=Microsoft Developer Network| archiveurl= http://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 |work=Distributed Programming with Ice |publisher=ZeroC, Inc.}}</ref>
|