|
-
May 11th, 2009, 02:25 AM
#1
Thread Starter
Lively Member
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.
-
May 11th, 2009, 02:41 AM
#2
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.
-
May 11th, 2009, 05:23 AM
#3
Hyperactive Member
Re: CPU USAGE Control.
Never use DoEvents. Throw this into a module and "Call MyDoEvents":
vb Code:
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSG
hWnd As Long 'the window handle of the app
message As Long 'the type of message (e.g. keydown)
wParam As Long 'the key code
lParam As Long 'not used
time As Long 'time when message posted
pt As POINTAPI 'coordinate of mouse pointer
End Type
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" _
(lpMsg As MSG, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function TranslateMessage Lib "user32" _
(lpMsg As MSG) As Long
Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" _
(lpMsg As MSG) As Long
Private Const PM_REMOVE = &H1
'The alternative function for DoEvents:
Public Sub MyDoEvents()
Dim CurrMsg As MSG
'The following loop extract all messages from the queue and dispatch them
'to the appropriate window.
Do Until PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) = 0
TranslateMessage CurrMsg
DispatchMessage CurrMsg
Loop
End Sub
Last edited by deathfxu; May 11th, 2009 at 07:45 AM.
Reason: clarification
-
May 12th, 2009, 12:47 AM
#4
Thread Starter
Lively Member
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.
-
May 12th, 2009, 01:51 AM
#5
Re: CPU USAGE Control.
 Originally Posted by ajay_vb
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.
-
May 15th, 2009, 07:43 PM
#6
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|