Library code snippets
How to simulate a status bar in a Web application
If you have a process that takes over 20 seconds or so on a Web application, you may want to let the user know the status of the process, e.g. if the user clicks a button to send out e-mails to 50 people, you may want to report to him on the screen when each e-mail goes out (probably about 1 per second) so that he knows there is progress taking place.
The problem with this is that web applications don't work in real time, instead the server finishes a process then sends out code to the client. However, with a little JavaScript, the ability to write and read to text files, and the ability to include files, you can create a simple process to keep the user abreast of the status of a process. This example was written in ASP.NET but you could probably recreate the concept it in any script language: basically it creates a page that refreshes itself every 10th second (constantly) and this page is RECREATED everytime the status changes, hence the user sees the change in status as your process runs.
StatusBar.aspx
-----------------------------
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Threading" %>
<script runat="server">
const int NUMBER_OF_ITEMS_TO_PROCESS = 10;
void Page_Load(Object sender, EventArgs e) {
//Thread.Sleep(1000);
UpdateStatus(GetCounter());
SetCounter(GetCounter() + 1);
if(GetCounter() > NUMBER_OF_ITEMS_TO_PROCESS) {
Response.Redirect("Finished.aspx");
}
}
private void UpdateStatus(int counter) {
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("status.htm"));
StreamWriter sw = fi.AppendText();
if(counter < NUMBER_OF_ITEMS_TO_PROCESS) {
sw.WriteLine(counter + ", ");
} else {
sw.WriteLine(counter + ".");
}
sw.Flush();
sw.Close();
}
public void SetCounter(int counter) {
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("Counter.txt"));
StreamWriter sw = fi.CreateText();
sw.WriteLine(counter.ToString());
sw.Flush();
sw.Close();
}
public int GetCounter() {
FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath("Counter.txt"));
StreamReader sr = file.OpenText();
int counter = Int32.Parse(sr.ReadLine());
sr.Close();
return counter;
}
</script>
<html>
<head>
<meta http-equiv="refresh" content=".1">
</head>
<body>
</body>
</html>
<!--#include file="status.htm" -->
StartProcess.aspx
-------------------------------------------------
<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
ClearStatus();
SetCounter(1);
Response.Redirect("StatusBar.aspx");
}
private void ClearStatus() {
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("status.htm"));
StreamWriter sw = fi.CreateText();
sw.WriteLine("Processing 10 Records...<br><br>");
sw.Flush();
sw.Close();
}
public void SetCounter(int counter) {
FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath("Counter.txt"));
StreamWriter sw = fi.CreateText();
sw.WriteLine(counter.ToString());
sw.Flush();
sw.Close();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<!--#include file="status.htm" -->
<br>
Process is finished.<br>
<br>
<a href="StartProcess.aspx">Start process again</a>.
</form>
</body>
</html>
Finished.aspx
--------------------------------------------
<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<html>
<head>
</head>
<body>
<form runat="server">
<!--#include file="status.htm" -->
<br>
Process is finished.<br>
<br>
<a href="StartProcess.aspx">Start process again</a>.
</form>
</body>
</html>
Counter.txt
-----------------------------
1
Related articles
Related discussion
-
Profile Class does not work after Translation
by converter2009 (1 replies)
-
what is the SQL Server Provider
by hayperaktib (1 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
Java Script, File uploading on ftp server using java script code
by h_c_a_andersen (2 replies)
-
sharepoint calendar web part with events from sql table
by converter2009 (2 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
However, I don't think the status is that neat and probably you need to do a lot of work to fit in with your application. For example you need to add your code into the GetCount function...or create a new thread... That's not I want.
However, I don't think the status is that neat and probably you need to do a lot of work to fit in with your application. For example you need to add your code into the GetCount function...or create a new thread... That's not I want.
You indcluded .htm file. but where is it to copy, if I want to try your code.
My requirement is that in asp.net application, I am getting 600 customer ids.
Code needs to loop thru all these 600 and open crystal report for each customer.
How can I show the status to the user about progress. It takes almost 2 hours to finish the task.
Thanks,
PK
This thread is for discussions of How to simulate a status bar in a Web application.