How to Sleep in ASP using ADO

By NeilO

Rate: (1 Ratings)

Here is a cheesy method I found for temporarily pausing execution in Active Server Pages (ASP) by abusing ADO and timeouts. None of this would be necessary if ASP, like WScript, simply had a Sleep() method.

Instructions

Difficulty: Moderately Easy

Things You’ll Need:

  • ASP Knowledge

Step1
Create an instance of "ADODB.Connection".
Step2
Set the ConnectionTimeout of the ADODB.Connection object to the amount of time you want to sleep in seconds.
Step3
Construct a connection string to a specific IP address rather than using a data source name (DSN). This should be pointing to a non-existant server and leave out the authentication information since it won't be necessary. I recommend pointing it locally (127.0.0.1) so you don't increase packet traffic on the network. Example string: "driver={sql server};server=127.0.0.1".
Step4
Call the Open() method with the connection string you made, but catch and ignore the exception thrown by it. If you're using VBScript you can use the horrific "ON ERROR RESUME NEXT", but remember to reset error handling afterwards.
Step5
You're done, that part of the code will cause configurable delay in your ASP.

Tips & Warnings

  • Subtracing 1 second from the ConnectionTimeout value set will generally get you closer to the mark.
  • Put this into a function (CheesySleep ?) so you can re-use it and replace it when you have a better way of doing it.
  • Just like your mama always said, putting sleeps in code is generally bad, but sometimes it just can't be avoided. At least this technique doesn't just spin on a loop checking the time (and thereby eat up all the CPU).
  • Seconds are not very granular.
  • Resulting sleep time is never exact; consider the ConnectionTimeout more of a "minimum" amount of time to wait ... you WILL be waiting longer.
  • I haven't fully tested if this results in BLOCKING other ASP requests or not.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to Sleep in ASP using ADO

eHow Member: NeilO

NeilO

Authority Authority | 3940 Points

Category: Internet

Articles: See my other articles

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.