Wait for Several Asynchronous Calls to Complete

Problem

You want to call multiple procedures asynchronously and suspend further processing until they are all complete.

Solution

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

Discussion

When you call the BeginInvoke method, you receive an IAsyncResult object that allows you to check the status of the thread and complete the request. In addition, the IAsyncResult interface defines an AsyncWaitHandle property that allows you to retrieve a WaitHandle for the asynchronous request.

The WaitHandle class defines three methods: WaitAll, WaitAny, and WaitOne. You can use the shared WaitAll method with an array of WaitHandle objects to wait for a group of asynchronous tasks ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.