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

doevents taking up processor usage

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
I'm trying to run a process every 5 minutes or so...

This code does this, however it uses the processor a lot. Doevents is taking up a lot of processor usage...

[lastrun].Value = Time

'infinite loop
While True
myDate = [lastrun].Value + 0.0035
Application.OnTime myDate, "LogInToSite"
While Time < myDate
Debug.Print DoEvents()
Wend
[lastrun].Value = myDate
Wend

Is there a way to do the same thing not hammering the processor. Having one excel sheet running using all processor power to do nothing doesn't seem ideal :)

Thanks,

Chris
 
Chrissirhc,
I don't think it's the DoEvents, the endless loop will hammer the processor all by itself.

You might look at [tt]Application.Wait[/tt], or for what your doing you may want to use a timer which may require an API call in Excel. Try searching this forum for [tt]SetTimer[/tt] or [tt]KillTimer[/tt] functions for more details.

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Application.wait is even more intensive at least during doevents I guess there is a moment where it frees up system resources for a split second.

I did a quick search for SetTimer. The posts don't seem to simple to me. I can't believe there isn't a simple command for waiting that doesn't take up any processor time.

Thanks,

Chris
 
Hmmm...

As CMP says it isn't DoEvents itself that is using the processor. DoEvents means hand over the processor (to Windows) for any pending tasks. When any pending tasks have been run control returns to the VBA loop. What the loop is effectively doing is using all spare processor time running (rather than letting it idle). Application.Wait is even worse than your loop. It is, effectively, a built-in loop without the DoEvents in the middle.

If you put an OnTime at the end of the LogInToSite process it will kick itself off every five minutes, something like this ...

Code:
Sub LogInToSite()
    'do whatever the routine does
    Application.OnTime Now + 0.0035, "LogInToSite"
End Sub

.. and all you need to do is start it once - any maybe code to stop it eventually subject to some condition/

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top