SQLServerCentral Article

Pause for a Minute

,

Pause For a Minute

Introduction

Recently I was writing a script that performed a number of things, including a brief service shutdown (not SQL). However there was an issue in shutting down the services because the applications had been built to bring up a dialog box if users were still connected to the application. However, if I waited about 45 seconds or so, the disconnections would occur and I could finish shutting down the app.

I complained to the vendor, but it didn't do much good. All my rants on poor programming practice were listened to and I was politely informed that was the way things were.

Arrrrrrrrrrggggggggghhhhhhhhhhhhhhhhhhhhhhhhh!!!!!!

Never fear, I came up with a workaround that I would like to share.

The Solution

I decided to dig through the documentation for VBScript and find something to help me. I first looked at the time function, which would give me the current time, but rejected that for the need to parse the values and do crazy time related math.

Then I stumbled on the timer function. This returns the number of seconds that have elapsed since midnight on the machine. Aha! Just what I need. I decided to implement this with a simple loop and some arithmatic to create my one minute timer. Here's the script:

Dim starttime, exittime, x
x = 0
' Save the start time
starttime = timer
' Calculate the end time
exittime = starttime + 60
' loop until the exit time is reached
do while timer < exittime
   x= x + 1
loop

That's it, a simple pause implemented.

Conclusions

A really simple script, but one that stumped a few admins around here. Not that you might need it, but this sometimes comes in handy if you are working with a process that might not respond immediately. You could use this to even retry some event after a minute has passed.

I know this could be improved and shortened, but I included a few extra lines for clarity. I've found it's helpful to not make leaps in programming, especially for beginners. I could have dropped this to a couple lines, but this seemed clearer. I could have made this a function and would have in any DTS script.

There is also a bug here. If you are planning on using this anywhere near midnight, DO NOT. It will not run correctly if you start it in the 11:58, 11:59 range. Better to schedule things well before or after this time.

As always I welcome feedback on this article using the "Your Opinion" button below. Please also

rate this article.

Steve Jones

©dkRanch.net August 2002


Return to Steve Jones Home

 

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating