Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Sleep

Status
Not open for further replies.

disord3r

Programmer
Apr 12, 2002
189
US
I while back a wrote a VBScript that used the Sleep() function to temporarily pause execution of the scipt. It was doing a bunch of file operations in a for...next loop on a rather slow system, and this was the only way I could allow for enough time for files to be closed before starting the next operation.

I have an ASP script that could benefit from this function also but it doesn't work. In the VBScript, it's called as WScript.Sleep(time_in_milliseconds), so I would imagine it's something supported by the Windows Scripting Host and not IIS. Is there any way at all to get this function to work in an ASP script, or is there another function similar to this that does work in ASP?

Thanks!
 
I use a function like this to delay the script

Code:
'Timed delay to allow for posting to database'
Dim StartTime, EndTime, WaitTime
' Get Start Time'
StartTime = Timer()

'Get End Time'
EndTime = Timer()

'Determine how long it took'
WaitTime = EndTime - StartTime

'Continue checking the elapsed time until it reaches 2 seconds'
You can change the 2 to however long in seconds you want the program to wait
Code:
do while WaitTime < 2
  'Get End Time'
  EndTime = Timer()

  'Determine how long it took'
  WaitTime = EndTime - StartTime
loop

Hopefully this is helpful

Why isn't phonetic spelled the way it sounds?


 
I found a way of doing it with a VB Com object (ActiveX DLL):

Use the following VB:
Private Declare Sub Sleep Lib &quot;kernel32&quot; _
(ByVal dwMilliseconds As Long)

Public Sub DoSleep(intMiliSeconds As Integer)
Sleep (intMiliSeconds)
End Sub

Make the dll, register on the server and call with your ASP page:

Set oSleep = CreateObject(&quot;projectName.className&quot;)
oSleep.DoSleep 100

Hope this helps someone, cos google searching for sleep is rather unhelpful!

--BB
 
There isn't a Sleep( ) built into VBScript because Microsoft knew people would abuse it like this!

The worst one is that &quot;loop until time goes by&quot; above. Where I work doing this intentionally would be grounds for dismissal. It burns up a whole processor as well as tying up an IIS thread. I'd like see to the reaction of an ASP web hosting company if you had a site with code like this and had more than an occasional hit! LOL

The second one isn't as nasty, at least it uses the Sleep( ) API. Though it may not eat CPU cycles like a short-circuit it does tie up an IIS thread and directly impact the server's ability to handle volume.

If you need to do something that doesn't block until completion (with a short timeout), write a VB component to handle it. For example if you need to use the Winsock control from ASP you should write a VB component to call Winsock and wait until a response comes back or a timeout expires. The hope is that most of the time you'll get data back quickly and your ASP script (and that IIS thread) can &quot;get on with its life.&quot;

Even if you use that &quot;buzz loop until 2 seconds&quot; thing, how do you know the thing you were waiting on actually got done? This is what's bad about both ways (loop like crazy or use VB to call Sleep( ) API) - you'll always hold up the thread 2 seconds even when the thing you are waiting on got done a long time ago.

But I guess sometimes you just get stuck without another solution, so you have no choice but use a nasty way out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top