Link to home
Start Free TrialLog in
Avatar of topman
topman

asked on

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
   percent.

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
    technique?

                   Thanks in Advance



Avatar of rwilson032697
rwilson032697

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

Cheers,

Raymond.
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"
      'YOUR LOOP GOES HERE
      Command1.Tag = ""
   End Sub


Hope this helps!


Cheers!
ASKER CERTIFIED SOLUTION
Avatar of Jeremy_D
Jeremy_D

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
pmwood,

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,
pmwood
pmwood:
Welcome!
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.

Cheers,
Jeremy
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.
Regards,
Peter
Avatar of topman

ASKER


  The Timer Control seems to do the trick.

                     Thanks to all