My VB5 programs are tying up CPU usage

Various tools for monitoring CPU usage
   show that my VB5 programs are tying
   up CPU usage between 90 and 99

I believe the problem is related to my
    technique for looping my programs.

For instance:

     If I wanted to print something
     every second on a form I would use
     a simple Goto loop with an
     embedded do loop timer.

Is this the cause of my high CPU usage
    and can you suggest a better

                   Thanks in Advance

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You should probably use a delay or sleep command for a specific time instead of a timing loop...


are you performing a DoEvents in your loop?  If not, placing a DoEvents statement in your loop will have your app dispatch any tasks that are waiting to execute.

When you incorporate DoEvents in your program, remember, you are dispatching your own program as well and you can get into stack problems by causing multiple loops in your program.

For example, lets say that you have a command button that contains a loop with DoEvents...

You click the button and the loop starts... While the loop is executing, you could click the button again.  If you do that, you will kick off the subroutine again, getting yourself into the loop again, and eventually running out of stack space.

To avoid this, you could simply set the Enabled property of the command button to FALSE when you start the loop, and then set it to TRUE when you end the loop.

You could also do something like this:

   Private Sub Command1_Click()
      If Command1.Tag = "RUNNING" Then Exit Sub
      Command1.Tag = "RUNNING"
      Command1.Tag = ""
   End Sub

Hope this helps!

Don't write your own delay-loops. That's exactly what an event-driven language on a (semi-)multitasking system is supposed to prevent.
Either use the Sleep API, or, in your case probably better, a timer-control.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

As Jeremy notes, this should not be done in an event-driven environment.

Ideally print as a response to a user request, such as pressing a button.

If you want to trigger something at regular intervals, user the Timer API or a Timer control to trigger an action at the required intervals.

A loop using the Sleep API to stop the application for x seconds in each loop will solve the CPU problem but is bad programming practice in the event driven env.

Doevents in a loop without sleep will not solve the CPU problem.

You are a new expert, and welcome... BUT... taking information from other comments and posting them as an answer is not cool...
My apologies, I'll be more circumspect in future.  BUT why did people post correct awnsers as comments?  Do they not know they're correct?  And if no-one then submits them as a solution, the question will never be closed ... or is there a lot I don't know in this game?

Open to enlightenment,
Some experts, including myself, answer questions with comments to keep them in the 'unanswered' section (don't ask me why, I just like that better, easier to find them back, I guess). Since it is possible to accept a comment as an answer this will not prevent the question from being closed.
Also keep in mind that a *lot* of questions concerning programming have more than one correct answer. I prefer leaving it up to the questioner to decide which (s)he thinks best for that particular situation.
This doesn't mean you have to do the same, when you're sure of your answer, you can post it as one if you like, but like mcrider said, don't copy&paste comments as answers.

Jeremy (& mcrider)-
Thanks for the response, incl. it's tone.  It's worth knowing that comments can be taken as answers.  I will tend to answer if I'm sure, but otherwise comment.  In my defense, I would have given much the same answer if there had been no comments, but I'll watch it in future.
topmanAuthor Commented:

  The Timer Control seems to do the trick.

                     Thanks to all
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.