• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Help with sleep


i want my application to call a certain function every 10 minutes. while it is not doing anything i want it to sleep.

do i need to import the system32 sleep function? and will this keep it from using any resources while its idle? i mean, i just dont wanna keep running thru a loop.
any code would be great.

thanks - js
1 Solution
Use a timer control and set the interval to 60000 (MSec)

Check for elapsed time >= 10 minutes and there you go:

Dim dteLastRun As Date

Private Sub Timer1_Timer()
  If DateDiff("s",dteLastRun,Now()) > 600 Then
    'Run my routine
    dteLastRun = Now()
  End If
End Sub
Use Timer control and on timer event calculate if the time is come to do something.
Sleep is also good.
Tim alredy explain :)
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

FYI--any use of the timer will create a thread that runs in the background, so it's still taking up processor time, but is minimal.  The only way to really overcome this is to have a hook into the Windows O/S that checks some flag every cycle through its processing, then "calls back" to the application at the proper time.  This is not worth the effort for something as simple as a timed event.
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
sleep is ok, but is seems to render your application 'not responding' on the task list if you sleep for a long peiod

sleep 600000   'for example (10 mins)
Sleep = Not good
Timer = vbOK And vbYes
If the sole purpose of your program is to execute this function periodically, and it need not do anything else, Sleep is fine and I suspect would use no more, perhaps less resource than the timer, if other actions need to take place then use the timer control.
>Timer = vbOK And vbYes

Ameba, you're funny, but...
vbOK And vbYes=0=False=Not True

So, if True=Good and False=Bad:
Sleep=Not good

I'd use the timer control whenever feasible, and this seems feasible, but (from help):

The maximum, 65,535 milliseconds, is equivalent to just over 1 minute.

To go more than one minute, you'll probably want a counter, something like this:

' Module-level variable
Private m_intMinuteCounter as integer

Private Sub Form_Load()
  ' or move this to wherever you enable the timer
  m_intMinuteCounter = 0
End Sub

Private Sub Timer1_Timer()
  m_intMinuteCounter = m_intMinuteCounter + 1
  if m_intMinuteCounter >= 10 then
    Timer1.Enabled = False
    PerformDesiredTask' 10 minutes has elapsed
  end if
End Sub
If you want to use a timer other than VBs timer you can use the two APIs SetTimer, KillTimer. Use of these two API's will not inccur any processing by your program as the Timer Fire event is a call back procedure.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now