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

How to avoid tying up 100% of your system resources?

Status
Not open for further replies.

PreacherUK

Technical User
Sep 26, 2002
156
NL
Hi all,

I seem to remember reading somewhere, many ages ago that there was a function within VBA that would allow a larger chunk of processor intensive code to 'sleep' just for a few milliseconds over and over every so many times through a loop (for example).

I'd love for this to be as simple as using the SLEEP command but I doubt it is. If anyone has any thoughts or even any thing that matches this criteria that would be very cool.

This is all in the attempt of avoiding tying up a PC while some labour intensive processing is going on within an MS Access database.
 
What's the objective here?

"Sleep" will halt execution of the code for the specified amount of time but will not return control to other processes.

"DoEvents" on the other hand does not halt current code processing but it does allow events external to that process to be executed while the process is running. For example
[blue][tt]
For n = 1 to 1000000
' Some time consuming processing
DoEvents
Next n
[/tt][/blue]
Will allow events that have been raised outside the For loop to be processed for each iteration of the loop. Without "DoEvents" this loop will have to complete all one million loops before you get control back.
 
I probably wasnt too clear. Its not other tasks and events within the db that need to run, its just general use of the PC that is running the db at the time.

If the db gets stuck into a very cpu intensive loop and consumes 100% (or close too) of the systems resources, such things as : opening a web broweser, a word document or spreadsheet are so slow as to be impossible to work with.

I keep remembering a small user written function that would interject a number of pauses (although I'm thinking more and more it was the 'sleep' command) into the loop that would cut cpu usage down to something around 50-75% (or more depending upon how liberal you were with teh pauses)
 
There is a Sleep API.
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub SomeRoutine()

   Do While (Condition = True)
      <Do a Bunch of Stuff>
[COLOR=green]      'Sleep for 5 Seconds  (5000 milliseconds)[/color]
      Sleep 5000
   Loop

End Sub


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks, I'll take a look and see if I can avoid blowing things up! :)
 
Thanks Cajun, thats what I was looking for. Have to see how well I can work that into things I've set up. Its useful when we are forced into producing a processor intensive desktop solution for something.

Thanks both of you :)
 
Hope it works out.

BTW, DoEvents returns control to the processor for work on any task from any thread, not just those from your own process.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top