Results 1 to 6 of 6

Thread: CPU USAGE Control.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    126

    CPU USAGE Control.

    Hi friends,
    Can anybody tell me the programming factors in vb6 or in general, to control the CPU usage. Basically I wanted to limit the program's CPU usage. So can anybody provide appropriate links or useful hints to control CPU usage?
    Thanks in advance.
    With Regards,
    AJAY.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: CPU USAGE Control.

    A normal vb program would rarely consume more than 2 - 3% of the CPU unless something is wrong there.
    The most common cause of high CPU usage I have found is doing something in a loop without a DoEvents at appropriate intervals. DoEvents basically processes the system message queue and gives time to your other processes to run.

    So, if this is the case with you I suggest you put a DoEvents call inside the loop and see if it drops the CPU usage.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: CPU USAGE Control.

    Never use DoEvents. Throw this into a module and "Call MyDoEvents":

    vb Code:
    1. Private Type POINTAPI
    2.  x As Long
    3.  y As Long
    4. End Type
    5.  
    6. Private Type MSG
    7.  hWnd As Long   'the window handle of the app
    8.  message As Long  'the type of message (e.g. keydown)
    9.  wParam As Long  'the key code
    10.  lParam As Long  'not used
    11.  time As Long   'time when message posted
    12.  pt As POINTAPI  'coordinate of mouse pointer
    13. End Type
    14.  
    15. Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" _
    16. (lpMsg As MSG, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, _
    17. ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
    18. Private Declare Function TranslateMessage Lib "user32" _
    19. (lpMsg As MSG) As Long
    20. Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" _
    21. (lpMsg As MSG) As Long
    22.  
    23. Private Const PM_REMOVE = &H1
    24.  
    25. 'The alternative function for DoEvents:
    26. Public Sub MyDoEvents()
    27.     Dim CurrMsg         As MSG
    28.  
    29.     'The following loop extract all messages from the queue and dispatch them
    30.     'to the appropriate window.
    31.     Do Until PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) = 0
    32.         TranslateMessage CurrMsg
    33.         DispatchMessage CurrMsg
    34.     Loop
    35. End Sub
    Last edited by deathfxu; May 11th, 2009 at 07:45 AM. Reason: clarification

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    126

    Re: CPU USAGE Control.

    Thanks Mr.Pradeep for your valuable suggestion. Also Thanks Mr.Deathfxu for your codebit. But can you please give the reason for why we should never use DoEvents?Can you be bit more clear about this?Thanking you,
    With Regards,
    Ajay.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: CPU USAGE Control.

    Quote Originally Posted by ajay_vb View Post
    Thanks Mr.Pradeep for your valuable suggestion. Also Thanks Mr.Deathfxu for your codebit. But can you please give the reason for why we should never use DoEvents?Can you be bit more clear about this?Thanking you,
    With Regards,
    Ajay.
    It would be wrong to say "never use DoEvents". Instead you would almost always use the DoEvents, unless the speed of the code is too much of a concern for your program, since the API version is much faster than the regular DoEvents. Though overuse of DoEvents might slowdown your program. It depends on your specific needs. However, beware of the pitfalls.
    Deathfxu seems to have been inspired by this thread, which I had created sometime back to compare the two things.
    http://www.vbforums.com/showthread.php?t=365050
    I think it might be worth reading for you.
    Last edited by Pradeep1210; May 12th, 2009 at 01:55 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: CPU USAGE Control.

    Egad, 2005. Never saw that, but that is precisely what I was referring to. :P

    The code bit above is just faster. If your program is slower and doesn't really need optimization, like Pradeep said, it's not really necessary.

    Just good coding practice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width