advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 1.8/5 | Rate this item | 26 users have rated this item.
Executing Database Commands Asynchronously with ADO.NET 2.0 (cont'd)
Executing a SQL Command Asynchronously from an ASP.NET Page
This section demonstrates how to execute a SQL command asynchronously from within an ASP.NET page. If you make an asynchronous SQL command invocation from within an ASP.NET application, and then return immediately within your code, you may not have the opportunity to include the results of the query execution in the data returned to the user—in other words, the page may complete before the query does. However, you can overcome this shortcoming by not releasing the currently executing thread. To accomplish this, you use the WaitHandle object, which lets you do some processing after making a SQL command execution call, and then block until the command has completed.
advertisement


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.

Previous Page: Executing Asynchronous SQL Commands  


Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He specializes in architecting, designing, and developing distributed enterprise class applications using .NET related technologies. He has coauthored a number of books in .NET related technologies and is a frequent contributor to leading technology related online publications.
Page 1: IntroductionPage 3: Executing a SQL Command Asynchronously from an ASP.NET Page
Page 2: Executing Asynchronous SQL Commands 
Please rate this item (5=best)
 1  2  3  4  5
advertisement