• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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

  • 3
  • 2
  • 2
  • +2
1 Solution
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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now