Server-Side ASP Sleep

While implementing an idea of web-based execution of Contributor Macros (see previous post), I’ve encountered the problem of some kind of progress-bar drawing in asp page.

When user hit’s asp page calling a Contributor Macro (which can take 15 min), he ought to see some action, telling him that “work is still going”. Mine idea was to draw sequential line of blue squares.

So the ASP page code will be like that:

set WScript = Server.CreateObject(“WScript.Shell”)
set oExec = WScript.exec(“c:\macro.bat”)
do while oExec.Status = 0
response.write(“<img src=’square.jpg’>”)
response.flush()
loop

That’s ok — but it draws a square every computer “tick” :) Can fill up a page in 2 secs.

A Sleep(5 secs) is needed in cycle body, just before response.flush()

But there’s no sleep instruction in ASP. Workarounds are drastic.

* Create an inner cycle to slow up execution. 100% CPU load — very nice.

* Install a WaitFor dll. What, on every client site to use?

* Use an SQL to call Ms SQL Timeout function. Fantastic.

So we had to find another workaround for sleep. It quite fits in above mentioned list.

1 Create a sleep.vbs file with: “Wscript.Sleep 200000″ (a very big number of your choice) line. A script to sleep forever.
2 From ASP page run “cscript.exe /T:your_time_to_sleep sleep.vbs” . So it just stops sleep.vbs execution on timeout of your_time_to_sleep.

Final code looks like:

set WScript = Server.CreateObject(“WScript.Shell”)
set oExec = WScript.exec(“c:\macro.bat”)
do while oExec.Status = 0 response.write(“<img src=’square.jpg’>”)
Set WShell = Server.CreateObject(“WScript.Shell”)
Wshell.run(“cscript.exe /T:your_time_to_sleep sleep.vbs”) response.flush()
loop

  • J R

    That’s a really dodgy solution.

    A better solution is to read the macro execution status from the database, and work out the percentage complete.

    You can use some simple HTML/Javascript to fill a div box with the percentage complete…or just use a freely available Progress Bar library.

    Your solution unfortunately gives no indication of how far along the progress is. It really only shows time elapsed waiting for it to finish.

  • ykud

    Hi John.

    Well, I agree that progress indication isn’t the best part of it.
    And, yes, I can read database for progress indication. Just the code will get a bit messy on the way, since there are Oracle and Ms servers out there. There’s also security involved, some have trusted databases signon and etc.
    So this thing was time consuming to write, we instead made “notification module”, sending out emails on macro runs. Had to choose feature set )

    And now we’ve got a simple script that periodically updates macro execution statistics (by reading the database). And that statistics are used for drawing % complete.

    That’s another workaround, but since 8.2 is already out and you can do the same thing with built-in tools, we don’t spend much time on improving this script.

    Thanks for comment )