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

Comments

  1. 04 Nov 2005 at 15:48
    I tried and it works. The htm file could be any htm files that you want write the status to. You can even use a blank htm page.  

    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.
  2. 04 Nov 2005 at 15:44
    I tried and it works. The htm file could be any htm files that you want write the status to. You can even use a blank htm page.  

    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.
  3. 01 Sep 2005 at 20:07
    Hi, I am not sure of following your code.
    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
  4. 01 Jan 1999 at 00:00

    This thread is for discussions of How to simulate a status bar in a Web application.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

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.

We'd love to hear what you think! Submit ideas or give us feedback