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

VB6 loop taking a bunch of resources.

Hi,

I need to build an eternal loop in a VB6 application that will be monitoring a variable change, of course this loop is taking a bunch of resources from the system, over 75% in task manager. Suggestions on how to perform this with minimal resources consumption are welcomed.

Regards,
0
lxfdirs
Asked:
lxfdirs
  • 2
  • 2
  • 2
  • +4
2 Solutions
 
GrahamSkanRetiredCommented:
Firstly, do make sure that you have one or more DoEvents in the loop.
Then consider using a timer so that the check is only done at intervals.
0
 
GS2907Commented:
Alternate way is to drop a timer control on your form and on its timer event, to the processing. You can also adjust the frequency of the timer.
0
 
Martin LissOlder than dirtCommented:
If you already have the code for the loop please show it to us,
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
aikimarkCommented:
If you're doing this for debugging purposes, you can do the same thing by setting a watch on the variable or an expression involving the variable.

You can also create dynamic breakpoints programmatically with one of the following methods:
If variable-related-condition Then
    Stop
End If

Or

Debug.Assert (variable-related-condition)

=============
What type of monitoring are you trying to accomplish?  In other words, what is the context for this question?
0
 
lxfdirsAuthor Commented:
Hi,

Appreciate your comments.
The loop is a simple one:
do
    doevents()
    if x = 3             ' x is set by another process which raises an event
        do something
    end if
loop while(1)

In my development station it takes 50% of the CPU. If I run the application on  Windows Server 2008 it takes 99%.


Regards,
0
 
aikimarkCommented:
how frequently can variable x change?

If it raises an event, why not put the check for the variable in the event code?
0
 
Martin LissOlder than dirtCommented:
What is the code behind "do something"? It must be that code that's causing the problem because your loop, while written in an unusual fashion, is pretty simple.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you ABSOLUTELY need to keep the code "in the loop" (can't use a timer for whatever reason), then add s SMALL call to Sleep() in your loop to throttle it back:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

...
    do
        doevents()
        if x = 3             ' x is set by another process which raises an event
            do something
        end if
        Sleep 50
    loop while(1)

Open in new window

0
 
amit_panjwaniCommented:
if you want to keep code reusable - either create a class with API calls or use custom ActiveX control which is lazy.

But MartinLiss does has a point - what is program doing behind loop .
0
 
lxfdirsAuthor Commented:
Appreciate all your comments, guys. A timer control with intervals worked well.<br /><br />Regards,
0
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

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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