O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Wait for One of Many Asynchronous Calls to Complete

Problem

You want to call multiple procedures asynchronously and suspend processing until any one call completes.

Solution

Retrieve the WaitHandle for each call, and use the shared WaitHandle.WaitAny method.

Discussion

The System.Threading.WaitHandle class provides a WaitAny method that accepts an array of WaitHandle objects and blocks until at least one WaitHandle is completed. When WaitAny returns, it provides an index number that indicates the position of the completed WaitHandle in the array.

The following example launches three calls at once. It then waits until at least one call finishes, displays the results, and then resumes waiting for one of the next two calls to complete. It uses an ArrayList ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required