ASP.NET Applications Using Wait Handles

Dheeraj Kumar Gunti

Reading Time : ( words)

The callback and polling models for handling asynchronous operations are useful when your application is processing only one asynchronous operation at a time. The Wait models provide a more flexible way of processing multiple asynchronous operations. There are two Wait models, named for the WaitHandle methods used to implement them: the Wait (Any) model and the Wait (All) model.

To use either Wait model, you need to use the AsyncWaitHandle property of the IAsyncResult object returned by the BeginExecuteNonQuery, BeginExecuteReader, or BeginExecuteXmlReader methods. The WaitAny and WaitAll methods both require you to send the WaitHandle objects as an argument, grouped together in an array.

Both Wait methods monitor the asynchronous operations, waiting for completion. The WaitAny method waits for any of the operations to complete or time out. Once you know a particular operation is complete, you can process its results and then continue waiting for the next operation to complete or time out. The WaitAll method waits for all of the processes in the array of WaitHandle instances to complete or time out before continuing.

Following is the example for handling asynchronous operations.

ThreadPool.QueueUserWorkItem - Queues a method for execution. The method executes when a thread pool thread becomes available.

AutoResetEvent - The AutoResetEvent class represents a local wait handle event that resets automatically when signaled, after releasing a single waiting thread.