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


Solve the Page Waiting Game with Threaded AJAX

ASP.NET offers a few solutions for handling long-running processes. One of the best methods is to combine threading with either a polling solution or AJAX.

eople hate to wait. In fact, when it comes to a computer, they hate to wait more than about 200 milliseconds. This can be a problem when your Web-based application uses a process that takes many seconds or even minutes. You can't just put up a progress dialog or a wait cursor.

Fortunately ASP.NET offers a few different solutions for handling such long-running processes, which vary depending on the level of interactivity required and the amount of complexity you are willing to handle. This article will start with a sample application to demonstrate the problem and run through two solutions: one using a simple polling technique, and the other a more advanced AJAX solution.

Be warned that there are some false steps to be found in the .NET framework. One in particular is the IAsyncHTTPHandler, which at first glance, looks like it will help with long web page requests. However, this asynchronous HTTP handler is designed to free up the processor when some task on a page takes some time but doesn't require any CPU. A good example is making a Web request in the middle of a page. In this case the asynchronous HTTP handler is very efficient.

The Problem
For this article, I'll be focusing on a different problem. In the sample application I have created a page that reports the current temperature, wind, and other weather elements for five different airports. It takes the web service about five seconds to get each data. So if I let the page run as shown in Figure 1 it takes almost a minute before the server returns a page—an unacceptable wait time for any user.

The HTML for the long-running page is shown in Listing 1. The page load event code creates a data set for the grid. Then it runs through the various airports and calls the web service to get the data. The method then stuffs the data from the web service into the data set and attaches it to the grid control (see Figure 2).

The WSDL for this web service is http://www.capeclear.com/AirportWeather.wsdl. It defines a bunch of different methods, only one of which I'll use: The getSummary method, which returns a block of data including the location of the airport, the wind, the sky conditions, the visibility, and more.

Figure 1. Waiting: The original sample applications takes almost a minute to load the page.
Figure 2. Basics: The Web service for the sample application performs a simple weather query.

Even a single request to this server takes a lot longer than a single page fetch should take. Another alternative is to have a thread run in the background to get the data, while pages on the front end monitor the output of the thread continuously.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date