To start, create a new ASP.NET Web site named
AsyncFeatures using VB.NET as the language. Rename the default page from
Default.aspx to
AsyncExample.aspx. Modify the code in the
AsyncExample.aspx file to look like
Listing 3.
 | |
| Figure 3. AsyncExample Web Form Output: The figure shows the output produced by the example asynchronous command execution code shown in Listing 3. |
The code in
Listing 3 uses the WaitHandle object's
WaitOne method to wait for the command execution to complete. The WaitHandle class also contains other static methods such as
WaitAll and
WaitAny. These two static methods take arrays of WaitHandles as parameters, and return when either
all the calls have completed, or as soon as
any of the calls have completed, depending on the method that you call. For example, if you are making three separate command execution calls, you can call each asynchronously; place the WaitHandle for each in an array, then call the
WaitAll method until they are finished. Doing that lets all three commands execute at the same time. It's also important to note that the
WaitOne,
WaitAll, and
WaitAny methods all optionally accept a timeout parameter value. Using the timeout option, you can specify the amount of time that you want to wait for a command to return. If the methods timeout, they will return a value of
False.
Figure 3 shows the output produced by
Listing 3.
As you can see, asynchronous execution opens up a raft of new opportunities and interesting scenarios that you can use to increase the performance and responsiveness of your .NET applications.