Where is the Throttle in a VB6 application?

Posted on 2005-04-11
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
    LVL 52

    Expert Comment

    by:Carl Tawn
    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 85

    Expert Comment

    by:Mike Tomlinson
    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    
    LVL 8

    Expert Comment

    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.
    LVL 8

    Expert Comment


    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 85

    Accepted Solution


    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.

    LVL 8

    Expert Comment

    Idle_Mind, you are right. Sorry, I misread the question.

    Author Comment

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    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…
    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…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now