Link to home
Start Free TrialLog in
Avatar of tkblack3
tkblack3

asked on

Can DOEVENTS consume the CPU or is it "not really using the processor"

A developer in my shop is insisting that DOEVENTS does not really use the processor because it is just queuing up openings to let other processes have access to the processor.

As VB 6 is not my strong suit, I am turning to the experts in the community on this.

Here is a snippet of code:

            frmMaint.Enabled = False
            CenterForm frmGrid
            frmGrid.Show
            Screen.MousePointer = vbDefault
            Do While Not frmMaint.Enabled
                DoEvents
            Loop
            frmMaint.SetFocus
            Unload frmGrid

When executed, I see the Task Manager register CPU utilization around 50% on the Citrix Metaframe server executing the code.  When multiple users execute the code, they each are allocated a percent of the CPU and max it out at 100%.

What is the value of this kind of DOEVENTS coding?  Is it needed to maintain the relationship between the multiple open windows on our application?  Is it really using all that CPU or is it just making the Taxk Manager think that it is busy?

This may be easy for someone, but between the couple of programmers we have on staff in our little shop, it is not resolved.

Thanks for the help.


SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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
SOLUTION
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
SOLUTION
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
You can use an enhanced doEvents..... It is discussed in this article.....

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=29735&lngWId=1

and also depending upon the situation you can use a Waitable timer.......(From Microsoft)
I personnaly use this waitable timer instead of Sleep API.....
http://support.microsoft.com/kb/q231298/

Imran Arshad
I agree that polling loops are generally a bad thing which is why I stated:

>> You may be able to accomplish your end goal by declaring custom events for your forms to raise and react to.

tkblack3, please give a bigger picture of what the app does and what the code is supposed to do and perhaps we can sugges a more elegant solution.

~IM
Avatar of Richard87
Richard87

ok, what about throwing your code to activate the seccond app in Form_Activate() Instead, bacause, this is the code that executes when the form get's enabled...
Avatar of tkblack3

ASKER

We are processing these comments trying to come to agreement on what is going on.

One guy maintains that the loop would not cause any delay on other processes/programs even though it drives the CPU usage up.  It is moving to a theory of how a CPU manages the queue of execution in general and then to how a Windows Server working with Citrix Server handles the queue.

From an application management standpoint, I am trying to understand why any of that is relavant.  I look at some very complicated applicaitons like spreadsheet and word processing and do not see that even when the user is typing, not much CPU is used. So how could someone begin to tell me that this kind of CPU utilization is "normal" and "acceptable" and that this little loop that lets the processor interrupt a program that seems to be doing nothing but looping around giving the processor a chance to do something else is the best way to write a program?

Sorry for the run-on sentence.

In small defense of the tight loop with a DoEvents, I once needed to utitlize a similar structure in an FTP application to implement time-out controls and other things.  I used a looping structure that ran continously while DataArrival() events fired.  This caused the CPU usage to ramp to 100% during the download process.  The files being downloaded were as big as 100MB (and sometimes bigger) so they took quite awhile to complete.  I never had any of my users complain that the system was unusable or sluggish while my app downloaded files.

In that case, I didn't want to use Sleep() to reduce CPU cycles because it interferes with receiving event notifications.

~IM
ASKER CERTIFIED SOLUTION
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
Is your code snippet accurate?  Is there nothing else in the loop besides

           Do While Not frmMaint.Enabled
                DoEvents
            Loop
SOLUTION
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
The application is an accounting package.  The loop is being used in relation to a data entry screen that lets the user "shell-out" to another screen to search for or setup a new vendor then return with the vendor value.

The loop is used in many other places in the code and I am working to get a list of occurances with justifications.

I am also looking to hire a programmer/architect to make a review of the code and recommend a plan for "armor plating" now that the core development is complete and we are in maintenance and enhancement mode.
When you say "Shell-out", do you literally mean a DOS shell at at least another application, or just another screen within your application?  If the former, the loop might be plausible but how does the other application affect the state of frmMaint.enabled?  If the later, they are full of S***.
SOLUTION
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
SOLUTION
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
RE:
1) Use Global variables in a Module
2) Use Public variables in the calling form
3) Make the modal form raise a custom event that is trapped by the calling form

I use
4) Pass procedures to a public function in the form.

I prefer this because 1) starts to create lots of global variables and doesn't encapsulate a form nicely, 2) if you use public variables in the form, you still have to make sure the form doesn't unload until you get your results and you end up doing a lot of frmSomeOtherForm.variablesHereAndThere outside the scope of a form, making maintenance more difficult.

3)  Would be good if your form can't be modal.
With (2), I meant public variables in the calling form.  The modal form can set those values before it unloads.

Method 3 works with Modal forms as well:

' --------------------------------------------
' Form1
' --------------------------------------------
Option Explicit

Private WithEvents f2 As Form2

Private Sub Command1_Click()
    Set f2 = New Form2
    f2.Show vbModal
End Sub

Private Sub f2_selectedID(ByVal id As Long, ByVal someValue As String)
    Debug.Print id, someValue
End Sub

' --------------------------------------------
' Form2
' --------------------------------------------
Option Explicit

Public Event selectedID(ByVal id As Long, ByVal someValue As String)

Private Sub Command1_Click()
    Unload Me
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' these values would come from the form itself normally...
    RaiseEvent selectedID(5, "Mike")
End Sub
Yep. (now we are getting off topic I think, but anyway...) RE: "I meant public variables in the calling form" I don't use 2) that way because then form2 has to reach back into form1 to set the public variables creating additional coupling, and if I want to also be able to call Form2 from a Form3, then I have to tell Form2 which parent form to write back into (a pain in MDI, causing additional coupling).  I like the procedures because there is only one point of coupling, the procedure call.  It's easy to do a search for, easy to change, and if necessary, I can do the MS trick of creating a new procedure ala FindEx(mynewsignaturevariables) to change up the way things work without breaking older code.

I know 3) works with modal forms, but my procedural method does not work with non-modal forms, that's why I like 3) for non-modal forms, though I can't say I've had a need to use it.
Thanks JohnB and Idle.  This continues as a topic in our shop and greatly enriched by your contributions.