RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Executing Database Commands Asynchronously with ADO.NET 2.0 : Page 3

Microsoft's upcoming ADO.NET 2.0 gives you the ability to execute SQL commands asynchronously, sidestepping the blocking effect of making lengthy database calls and letting you make multiple queries simultaneously, using polling, WaitHandles, or delegate notification to know when query execution completes.

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.

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.

Thiru Thangarathinam works at Intel Corporation in Chandler, Arizona. He's a Microsoft MVP who specializes in architecting, designing, and developing distributed enterprise-class applications using .NET-related technologies. He is the author of the books "Professional ASP.NET 2.0 XML" and "Professional ASP.NET 2.0 Databases" from Wrox press and has coauthored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date