Solved

Help with sleep

Posted on 2002-03-28
10
246 Views
Last Modified: 2012-05-04
hello-

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
0
Comment
Question by:jeremiahscott
10 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6903072
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
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6903076
Use Timer control and on timer event calculate if the time is come to do something.
Sleep is also good.
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6903079
Tim alredy explain :)
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6903120
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.
0
 
LVL 2

Expert Comment

by:DanielBlais
ID: 6903135
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:deighton
ID: 6903146
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)
0
 
LVL 15

Expert Comment

by:ameba
ID: 6903606
Sleep = Not good
Timer = vbOK And vbYes
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6903707
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.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6903744
>Timer = vbOK And vbYes

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

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

-----
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
0
 
LVL 5

Expert Comment

by:RainUK
ID: 6913296
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.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now