article

Heavy usage w/o DoEvents (the proper way)

Email
Submitted on: 2/2/2015 6:17:00 PM
By: CR Hough (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: VB 5.0, VB 6.0
Views: 1655
 
     Never use DoEvents again. This is a standard C/C++ Message Pump. Compare each of these three methods against each other (including DoEvents) and watch your CPU usage. The code is pretty self-explanatory I think.

This article has accompanying files
 
				  Put this in the General Declarations area for your module:
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
Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long

Private Const PM_NOREMOVE = &H0
Private Const PM_REMOVE = &H1
Private Const WM_QUIT = &H12

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Type MSG
hwnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
point As POINTAPI
End Type


Put the following in your sub main (after setting blRunning = True):
'***********************************************************
'*** Try commenting and uncommenting each of these three ***
'*** methods. Watch your CPU usage when you run each of ***
'*** them. Then go back to using GetMessage. ***
'***********************************************************
Do While blRunning
'*** use the PeekMessage version if you want to use 100% CPU
'*** frex to do background processing that doesn't rely on
'*** windows messages
'If PeekMessage(Message, 0&, 0&, 0&, PM_REMOVE) Then
' Call TranslateMessage(Message)
' Call DispatchMessage(Message)
'End If

'*** use the GetMessage version if you only want to do
'*** processing if there's a message
If GetMessage(Message, 0&, 0&, 0&) Then
Call TranslateMessage(Message)
Call DispatchMessage(Message)
End If

'*** the 'pure vb' poor way to do this
'DoEvents
Loop

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.