|
-
Dec 7th, 2004, 08:34 PM
#1
Ok noobies, DoEvents is slow!!! Here's are faster methods:
Note to Moderators: If possible, can you make this a sticky?
That's right people. And it's no wonder why C++ programmers say VB is too slow. It's because the majority of the functions that come with VB are too slow, especially DoEvents.
What if I told you that you can not only do methods faster than DoEvents, but tame DoEvents and control it to your benefits. You tame it using the following API's
Code:
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
'Private Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
And here's the code in action:
Note: This is not my code
DoEvents Tamer.zip
Ok now that you know how to tame DoEvents, let's do faster methods. How about the PeekMessage API:
Note: Also not my code:
PeekMessage API.zip
But are their faster methods. YES!!!
Code:
Private Declare Function GetInputState Lib "user32" () As Long
The GetInputState API is a whole lot faster. Unfortunately though, it only does events when an event has occured. Which means if a user does not press the keys on the keyboard or use the mouse, it will be like not using DoEvents in a loop until an input event has occured.
Here's a documentation on GetInputState as well as another API I will be discussing. And no I didn't write it:
DoEvents 100% Faster.doc
The best damn API I have ever used for DoEvents is GetQueueStatus
Code:
Private Declare Function GetQueueStatus Lib "user32" (ByVal qsFlags As Long) As Long
It happens to literally be 10 times faster than a regular DoEvents alone. Here is MY code, one I've wrriten myself, finally. This one does a simple performance test:
GetQueueStatus API.zip
And what I've learned in this performance test is this. Results may vary from computer to computer:
DoEvents
IDE - 192136
Exe - 296140
Comments: Slow, slugish, and ugly for realtime.
PeekMessage
IDE - 688950
Exe - 735468
Comments: Getting there. This is the method C++ commonly uses.
GetInputState
IDE - 965230
Exe - 1113434
Comments: Problem with this is that it's only active when an event has occured. With this I just simply held a key down.
GetQueueStatus
IDE - 947204
Exe - 1101420
Comments: It is fast and reliable. And you can add more events for it to check for too. It'll still be fast
Now are there other methods? There should be. Although I don't know myself. I hope you enjoyed this lengthy tutorial. I know I did! Comments and suggestions please.
Last edited by Jacob Roman; Oct 16th, 2005 at 11:03 AM.
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
|