Community Pick: Many members of our community have endorsed this article.

Classic ASP script for Pausing a script, without sending the CPU to 100%

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update.

The Original Issue

This was written to stop an update script from using all of the CPU itself. It was slowing down all of the other services on the server, as it took 20 minutes or more to run. In my case, I was updating a MySQL database with 45000+ lines, from a CSV file, with far too many columns. There was a lot of processing for each record, and the data had to be checked "backwards and forwards".

The Need

Before you implement this code, make sure you actually need it. What I mean by this is ensure you are not causing the issues yourself. Many times, a re-written SQL statement will save bundles of time. In other cases, you may be better off sending something back to the client, with a Meta Refresh (another article coming soon?). I would suggest only using this if you really need it...

The Mistakes

I have seen many options for causing a delay. Most of them involve some thing like:

for i = 1 to 1000000
                      '' ## OR
                      Counter = 1000000
                      Do While Counter > 0
                      	Counter = Counter - 1

Open in new window

As anyone who has tried to run one of these will tell you, it just sends the CPU to 100%. And hopefully, you can see why. The server is trying very hard to get to the end of the script, burning clock cycle after clock cycle.

Another variation of this is to use the ASP "Timer". This basically has the same effect.

My Way...

If you have access to the WScript.Shell, we can use "PING" to cause a delay. The PING command can take a switch "-w" which tells it how long it should wait for a response, before giving up. Therefore, my script builds a PING command, (using "-n 1" to only ping once) which I trust will fail, and return "no-joy" after the pre-described number of seconds.

As the -w switch of PING takes milliseconds rather than seconds, the script deals with that too...

'' ## Waits specified no. of secs and then continues (roughly).
                      '' ## Requires IUSR Access to PING
                      Function WaitASec(Input)
                          Dim Seconds
                      	Seconds = Input * 1000
                      	Set objWShell = CreateObject("WScript.Shell")
                      		Set objCmd = objWShell.Exec("C:\WINDOWS\system32\PING 0." & Hour(Now()) & "." & Minute(Now()) & "." & Second(Now()) & " -n 1 -w " & Seconds)
                      			strPResult = objCmd.StdOut.Readall()
                      		set objCmd = nothing
                      	Set objWShell = nothing
                      End Function

Open in new window

You will also see that I have programmed the ping to ping a different IP each time. This is for two reasons:

I had issues with "caching" of failed results.

I did not want to annoy any one in particular...

The up-shot of all of this is the ASP script waits patiently, whilst the "PING" is doing it's "thing" (sorry). It does not max the CPU, or hold-up anything else.

The Right Way...

Whilst my script works, and it is the only working solution I have seen to date, I am sure there must be a better way. If we can access the command line like this, I am sure we can run a batch file (those with server access anyway...).

I expect some comments from "Batch Guys" telling us what we could put in a batch file, for better results. As the timing on this is only approximate.

That said, this is not about accuracy, this is about free-ing the processor to do something else once in awhile!


All you need to do now, is interrupt your script once in a while. How you do that is up-to-you. Partly because only you know your code, but mainly because it is your choice of how often to stop, and for how long.

This can be implemented by the previously mentioned "timer" function, or in my case every 50 records.

You will also need to watch out for the increased time it takes to run the script. In my case it added about 10 minutes, which was not a problem for me, but it may exceed your script.timeout...

Happy Coding:



Comments (2)

Most Valuable Expert 2012
Top Expert 2014

Hi G_H, thanks for the article. I'm sure many people will find this tip helpful.  I have one minor alterative that should also work:

            Set objCmd = objWShell.Exec("C:\WINDOWS\system32\PING 0." & Hour(Now()) & "." & Minute(Now()) & "." & Second(Now()) & " -n 1 -w " & Seconds)
            strPResult = objCmd.StdOut.Readall()

Could be more simplified to:
            objWShell.Run "C:\WINDOWS\system32\PING 0." & Hour(Now()) & "." & Minute(Now()) & "." & Second(Now()) & " -n 1 -w " & Seconds, 0, True

            objWShell.Run "cmd /c %WINDIR%\system32\PING 0." & Hour(Now()) & "." & Minute(Now()) & "." & Second(Now()) & " -n 1 -w " & Seconds, 0, True

as you don't need to use Exec, as the output does not need capturing.



classicaspfaq has another way, but it requires a connection to an SQL Server

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.