Walkthrough: Tracking Server Progress from an ASP.NET Client Application
Martin Tracy
Visual Studio Team
Microsoft Corporation
July 2003
Applies to:
Microsoft® ASP.NET
Microsoft® Visual C#® .NET
Microsoft® Visual Studio® .NET
Summary: Demonstrates how to visually track the progress of an ASP.NET code-behind server application from a client Web application. (2 printed pages)
When a server application takes a long time to complete, the user watching a browser window might wonder if the application has hung. A progress bar would keep the user informed about the progress of the application, but progress bars are difficult to implement in a Web application. The Internet is primarily a "pull" technology, that is, Web pages are served on request. There is no easy way to "push" progress reports to the user.
It's possible, however, to write a client script that requests pages periodically from the server. The server can cooperate by altering each requested page to show the progress of the application. When the application is finished, the server delivers the final page without the client script.
Before you start, you must have the following software installed on your computer:
- Microsoft® Windows® XP with SP1 and all hotfixes.
- Microsoft® Internet Information Services (IIS).
- Microsoft Visual Studio .NET.
Be sure you can create a simple Microsoft Visual C# ASP.NET Web application before attempting the walkthrough.
In this walkthrough, you will use the Page.RegisterStartupScript method in a Visual C# code-behind Web application to force the immediate submission of a client Web page. While the user reads the client Web page, the server application undertakes the next section of an application, simulated by the Thread.Sleep method. The final page is served without the startup script.
Start Visual Studio .NET and create a new Visual C# ASP.NET Web application.
Insert this code into the Page_Load method:
private void Page_Load(object sender, System.EventArgs e) { // Count backward from 5 to zero. int counter; if (!IsPostBack) counter = 5; else counter = ((int) ViewState["counter"]) - 1;
if (counter < 0) { Response.Write("Mission accomplished!"); return; } ViewState["counter"] = counter; Response.Write("Countdown: " + counter); // Force immediate postback. string sCommand = "<script type=\"text/javascript\">" +
"document.forms[0].submit()</script>"; RegisterStartupScript("Progress", sCommand);
// Simulate the the next piece of the application.
System.Threading.Thread.Sleep(1000);
}</pre>
- Press F5 to start the Web application under the debugger. The browser displays a countdown from five to zero, finishing with the message, "Mission accomplished!"
For more information, see the following topic:
ASP.NET QuickStart Tutorial (http://samples.gotdotnet.com/quickstart/aspplus/)