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!

100% of CPU usage in infinite loop 1

Status
Not open for further replies.

afryer

Programmer
Mar 9, 2004
207
GB
Hi All,

I have written an application in VB, which runs a constant timer to keep track of various events. Basically what happens is when the application is started a function is called from the Form_Initialize event to start the timer. The timer is in an infinite loop of the following format:

Do
DoEvents ' to allow user to exit

' code to process timer
Loop

This all works fine, but what I am finding is that the CPU usage for the application uses pretty much all of the available CPU - if nothing else is running then it uses 99%. After investigating on the net I can find information about it and it is the loop that is causing the excessive CPU usage, but I need to find someway of reducing this as it is obviously unacceptable for it to be using so much.

Any help would be appreciated.

Thanks



Andrew
 
Try this format:

Code:
Private Declare Function GetInputState Lib "USER32" () As Long

Do
   If GetInputState = 0 Then

      'place your procedures here

      DoEvents
   Else
   End If
Loop
 
Hi,

I combined this with a Sleep (10) call in the loop and this has reduced the CPU usage from 99% to 1%, which is considerably better!

Thanks


Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top