Design Pattern for Asynchronous Method Calls

The .NET Framework defines a design pattern for calling methods asynchronously. It is important to use a common design pattern to avoid confusion among developers. This pattern dictates that there are two asynchronous methods for every synchronous method. Specifically, for every synchronous method there is a Begin and an End asynchronous method. The Begin method is called by a client to initiate the execution of the method. With this call, the client tells the method to begin processing the method and control is returned immediately to the client. There are a few options for determining if the call has been completed. These include specifying a callback method that will be called when the execution has been completed or performing periodic checking to see if the execution has completed. After it has been stipulated that the execution has completed, the End method is called by the client. The End method performs the retrieval of the results of the Web Service method call.

NOTE

Some functions need to send information back to an application, and callback methods can be used to accomplish this. A callback method provides a way for client applications to complete an asynchronous operation by supplying a callback delegate when the operation is initiated. The callback delegate referenced by a call could then receive any notification from the execution methods. With asynchronous calls, the callback method is notified that the execution has completed.


The Begin and End methods are exposed by the proxy object and are created for each public Web Service method exposed by the Web Service. (Generation of the proxy is discussed in Chapter 2, “Consuming Your First Web Service.”) This is also true, even if there is only a synchronous implementation of the Web Service method.

Table 8.1 describes the synchronous and asynchronous methods.

Table 8.1. Description of Methods Exposed in the Proxy Class
Method Name in Proxy Class Description
<WebServiceMethodName> Invokes a Web Service method named <WebServiceMethodName>
Begin<WebServiceMethodName> Begins an asynchronous call to a Web Service method named <WebServiceMethodName>
End<WebServiceMethodName> Ends an asynchronous call to a Web Service method named <WebServiceMethodName>, retrieving return values from the Web Service method

There are two built-in mechanisms available in the .NET Framework for the client to find out whether the method has completed processing the call and is ready to produce the results:

  • The client passes a callback function into the Begin method. The callback function is called when the method has completed processing.

  • The client waits for the method to complete. The WaitHandle class exposes some methods that can be used to allow the client to wait for the method to complete its execution. In this case, the call made by the client will return an object implementing the IAsyncResult interface.

    The IAsyncResult object can be used to receive information about the status of the asynchronous call. It also contains an AsyncWaitHandle property of type WaitHandle. The WaitHandle object implements methods that support waiting for the synchronization object to be signaled. The possible signals are WaitOne, WaitAny, and WaitAll. These are discussed in more detail in the section “Using the WaitHandle Methods,” later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.179