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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep without locking up program

Status
Not open for further replies.

PockyBum522

Programmer
Jun 10, 2003
239
US
Does anyone have: Something like pause (it doesn't cause the program to stop) But like sleep in that I can give it a time to pause in milliseconds.

Thanks.
 
?Timer control?

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
I wanted to make it into a function I can put in a module, I may be lazy, but I thought it would be useful. That's why I wanted to avoid a timer.
 
His exampe (the post above this one) is a great way to do milliseconds. If you want to do a longer time (i.e. minutes or hours), you'll want to use Time$ or Time and possibly Date$ or Date. Note: Time$/Time and Date$/Date give you different formats each and are not the same. Simply extract the hours and minutes and check them against a future time you want to wake up the app. I wrote an app that checks my IP every minute and if it differs from the last check it notifies me. I've also written an app that checks hard drive space for certain paths at certain intervals. These rest on the taskbar and do their thing at set intervals. It's pretty simple to implement. :eek:)

CGFiend
[There's no chr$(27)!]
 
Try putting this in a module....
Public Function sleep(doze As Integer) As Integer
Dim x As Long
x = Timer
Do Until (Timer - x) >= (doze / 1000)
Loop
sleep = (Timer - x) * 1000
End Function

It returns the actual time it took in milliseconds

sleep 3000 Will delay approximately 3 seconds
 
I figured I'd post a few lines of code for an app I have sitting on the taskbar and checking minute intervals. Maybe you'll find it useful...

Step 1: Create a timer control on your form and disable it by default. Set it for however m/s you want it to check the current time against the wakeup time. This is very low CPU intensive.

Step 2: From form_load() or a sub of your choosing set initial parameters, then exit the sub (i.e. app sits idle) and enable the timer which does a compare of current time to interval in minutes:

Private Sub Form_Load()
'Store current minute

StartMin = Mid$(Time$, 4, 2)


'add code here to create a taskbar icon

'Enable timer and sleep the app

Timer1.Enabled = True

End Sub


Private Sub Timer1_Timer()
'Compare current time to a preset interval of x minutes.
'and the start time of the app. If the current time
'is past the interval then "wake up"

If Mid$(Time$, 4, 2) >= StartMin + CInt(txtInterval.Text) Then

'add main code to run here
'also add any taskbar icon change if
'conditions warrant

'Store the new current minute

StartSec = Mid$(Time$, 4, 2)

End If
End Sub


CGFiend
[There's no chr$(27)!]
 
Woops! In the IF/THEN of my code it should be StartMin = Mid$(Time$, 4, 2), not StartSec = Mid$(Time$, 4, 2). lol

CGFiend
[There's no chr$(27)!]
 
SetWaitableTimer works like I want, the only problem is I can't get it to do milliseconds. It obviously does milliseconds by the way it's been referred to, but using the code:

Wait 10

It waits 10 seconds not 10 milliseconds.

I don't think wait is what I want to be using, I'll go take another look at the SetWaitableTimer code and see if I can figure it out. If anyone wants to tell me in the meantime, feel free.

Thanks.
 
Since no link to a SetWaitableTimer thread has been provided anywhere here, I'm assuming that you did a keyword search and found thread222-610857

If so, then you want to change

dblDelay = Cdbl(lngNumSecs) * 1000 * 10000

to

dblDelay = Cdbl(lngNumSecs) * 10000
 
Last thing, it doesn't seem to like me using a variable for a time to wait:

Wait x

If I can't fix this, it's not a problem. I just wanted to know why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top