Where is the Throttle in a VB6 application?

Posted on 2005-04-11
Medium Priority
Last Modified: 2013-12-26
Hello Experts,

Is there any way to control how much resource my VB app takes?  Sometimes my app takes 99 percent CPU. While doing a loop or sleeping my app can take power from all other apps running on this computer. I dont need my app to run so hard or take so much juice. Any ideas?

Question by:dominica526
  • 2
  • 2
  • 2
  • +2
LVL 52

Expert Comment

by:Carl Tawn
ID: 13759898
If you have a big intensive loop then make sure you have a call to DoEvents in it. This will allow your app to yield an allow other apps to do work.

Hope this helps.
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13759947
DoEvents will allow your app to remain responsive but it won't reduce the CPU usage in a tight loop.  To alleviate that, place a call to the Sleep() API with a short duration of about 50 milliseconds:

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Sub Foo()
        Dim i As Long
        For i = 1 to 100000
            ' doing something in some kind of loop...
            Sleep 50 ' Keep CPU usage from ramping to 100%
            DoEvents ' Keep App Responsive
        Next i        
    End Sub    

Expert Comment

ID: 13760227
Even better than sleep would be the following:

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Sub Sleep(Time As Long)
'Time is in SECONDS
Dim StartTime As Long
StartTime = GetTickCount()
Do Until GetTickCount - StartTime >= (Time * 1000)
End Sub

But note that DoEvents WILL slow down your loops.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 13760421

Another thing you could do along with the above items is to change the process priority of your application. Process priority will determine how much CPU time your app will get vs. other applications that are more important (like OS related processes).

You can check out a little code here that will show you how to change it's priority:


LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 13761938

The whole point of the question was how to REDUCE CPU usage.  Your Do..Loop will cause CPU usage to ramp to 100% without a call to Sleep() in it as I have shown.

Also dominica526, your statement:

    "While doing a loop or sleeping my app can take power from all other apps running on this computer."

isn't quite correct.  A tight loop may steal CPU cycles from other apps but if the app is sleeping via the Sleep() or one of the various WaitXXX() of MsgWaitXXX() APIs then it will use NO CPU cycles at all.


Expert Comment

ID: 13766860
Idle_Mind, you are right. Sorry, I misread the question.

Author Comment

ID: 13768408
These are all some truly great ideas that I must play with. I am always amazed at the level of help received by this group. I will let you all know what I come up with.


Author Comment

ID: 13777996
Thank guys for all the ideas, however sleep for a few mils has little effect on loop performance and my app is way easy on the CPU.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

621 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