Wait Handles

With a wait handle, you can cause your program to wait until any one request in a batch of requests is complete or until all the requests have finished. All you need to do is use one of the shared methods of the System.Threading.Wait­Handle class and provide it with an array of WaitHandle objects. You can retrieve a WaitHandle from the IAsyncResult.AsyncWaitHandle property.

Listing 6-5 shows an example that starts several XML Web service methods asynchronously and waits for them all to complete. Note that for this code to work as written, you need to import the System.Threading namespace.

Listing 6-5 Waiting for multiple asynchronous methods to complete
 ' Call three methods asynchronously. Dim Async1, Async2, Async3 As IAsyncResult Async1 = Proxy.BeginGetStockQuote("MSFT", Nothing, Nothing) Async2 = Proxy.BeginGetStockQuote("AMZN", Nothing, Nothing) Async3 = Proxy.BeginGetStockQuote("EGRP", Nothing, Nothing) ' Create an array of WaitHandle objects. Dim WaitHandles() As WaitHandle = {Async1.AsyncWaitHandle, _                   Async2.AsyncWaitHandle, Async3.AsyncWaitHandle} ' Wait for all the calls to finish. WaitHandle.WaitAll(WaitHandles) ' You can now retrieve the results with Proxy.EndGetStockQuote(). 

You also can instruct a wait handle to block the thread until any one of the method calls has finished. This can be a useful technique if you need to process a batch of data from different sources and the order in which you process it is not important, as shown in Listing 6-6.

Listing 6-6 Waiting for at least one asynchronous method to complete
 ' Call three methods asynchronously. Dim Async1, Async2, Async3 As IAsyncResult Async1 = Proxy.BeginGetStockQuote("MSFT", Nothing, Nothing) Async2 = Proxy.BeginGetStockQuote("AMZN", Nothing, Nothing) Async3 = Proxy.BeginGetStockQuote("EGRP", Nothing, Nothing) ' Create an array of WaitHandle objects. Dim WaitHandles() As WaitHandle = {Async1.AsyncWaitHandle, _                   Async2.AsyncWaitHandle, Async3.AsyncWaitHandle} ' Wait for at least one call to finish. WaitHandle.WaitAny(WaitHandles) ' You will need to check the IAsyncResult.IsCompleted property ' to determine which calls are finished. 

The WaitHandle class also provides a single instance method, WaitOne, which just blocks the thread until the call is completed. It's analogous to using EndInvoke, except it doesn't return the information. (A separate EndInvoke call is still required.)

 ' Call three methods asynchronously. Dim Async1, Async2, Async3 As IAsyncResult Async1 = Proxy.BeginGetStockQuote("MSFT", Nothing, Nothing) Async2 = Proxy.BeginGetStockQuote("AMZN", Nothing, Nothing) Async3 = Proxy.BeginGetStockQuote("EGRP", Nothing, Nothing) ' Wait for them all to finish. Async1.AsyncWaitHandle.WaitOne() Async2.AsyncWaitHandle.WaitOne() Async3.AsyncWaitHandle.WaitOne() 

Finally, the WaitHandle class also provides overloaded versions of all these methods that enable you to specify a maximum wait time. If this time is reached and the method hasn't completed, your code continues. You specify the wait time with an integer representing a number of milliseconds or a TimeSpan object.

 ' Call one method asynchronously. Dim Async1 As IAsyncResult Async1 = Proxy.BeginGetStockQuote("MSFT", Nothing, Nothing) ' Wait for it to finish (or time out after 10 seconds). Async1.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(10), False) 


Microsoft. NET Distributed Applications(c) Integrating XML Web Services and. NET Remoting
MicrosoftВ® .NET Distributed Applications: Integrating XML Web Services and .NET Remoting (Pro-Developer)
ISBN: 0735619336
EAN: 2147483647
Year: 2005
Pages: 174

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net