I encountered an interesting problem today. A function that needed to be run on a webserver (as an aspx page) but it did a huge amount of processing based on the users input - lots of file reads and database updates. It was dragging performance on the rest of the site down while it ran.

In a normal scripting environment for a Windows application it's easy - the sleep({seconds to sleep}) command is your friend. But you can't do it in VB.NET for a webpage.

Unless, that is, you have SQL Server 2005 or another database that supports the WAITFOR DELAY {time to delay} command. 

Sub sleep(sec as integer) Dim con_timeout, sql ' indicate a number of seconds, up to 59 ' if you need more than 59 seconds, you will need to adjust the SQL below If sec > 59 then sec = 10 End if ' make sure timeout doesn't expire! ' assumes we have an already active connection to the database called "con" con_timeout = con.commandTimeout con.commandTimeout = sec + 5 sql = "WAITFOR DELAY '00:00:" & right("00" & cstr(sec),2) & "'" con.Execute(sql,,129) ' put the timeout back con.commandTimeout = con_timeout End Sub

Now the script may take a fair bit longer to operate, but the judicious use of sleep(2) within the more tightly looping and time consuming bits of code means that the server can spare some cycles to answering other demands on it.

Of course, you then need to make sure that your script doesn't timeout, so careful use of Server.ScriptTimeout to balance it is also called for