Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Optimization] Optimized DoEvents
#1
As you probably know, DoEvents is a very useful function built-in to VB allowing us to give our applications some 'fresh air' when they are doing intensive operations, such as loops. DoEvents works by checking for any other event that might need to run at the time, and then runs it. The only problem is DoEvents checks many useless events that we might not even need.

Basically, we are 're-making' the DoEvents function to check only for certain messages, such as keyboard input, mouse input and painting the screen.

Just copy paste the following in any module :

Code:
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Public Const nLng As Long = (&H80 Or &H1 Or &H4 Or &H20) + (&H8 Or &H40)

Public Sub NewDoEvents()
    If GetQueueStatus(nLng)  0 Then DoEvents
End Sub

To make sure this actually optimized the function, I decided to do a benchmark. I opened up a blank VB app, put two controls and basically made a loop that would set the caption of one of the buttons to the current loop index. This is the code I used for the benchmark :

Code:
Dim I As Long, Tick As Long
Tick = GetTickCount
For I = 1 To 1000000
Command1.Caption = I
Next I
Text1.Text = (GetTickCount - Tick)

Tick = GetTickCount
For I = 1 To 1000000
Command1.Caption = I
DoEvents
Next I
Text2.Text = (GetTickCount - Tick)

Tick = GetTickCount
For I = 1 To 1000000
Command1.Caption = I
NewDoEvents
Next I
Text3.Text = (GetTickCount - Tick)

And the results were :
Code:
Test 1
No DoEvents whatsoever - 15694 Ticks
Old DoEvents - 97875 Ticks
New DoEvents - 17004 Ticks

Test 2
No DoEvents whatsoever -7238
Old DoEvents -109528
New DoEvents - 7800

So as you can see, it definitely is a fairly important improvement to the function, and should let your loops that use DoEvents run much faster while still allowing user input Smile
Reply
#2
DoEvents are generally bad and tend to cause a lot of errors.
Reply
#3
Joost Wrote:DoEvents are generally bad and tend to cause a lot of errors.

O rly? Link to an article/source? I've never had any problems with DoEvents..
Reply
#4
http://www.codinghorror.com/blog/archives/000159.html

http://www.codinghorror.com/blog/archives/000370.html

http://www.codinghorror.com/blog/archives/000055.html

+ common knowledge
Reply
#5
If you read, it says it is evil because it "processes all of the messages in the message queue, not just paint messages". We are fixing that by making it only handle input and repainting messages. Thus those articles don't apply on this tutorial. Smile
Reply
#6
Fine, I was too lazy, and only checked one website. Regardless of that, DoEvents are bad, and I'll do my best avoiding them in my code as much as I can.
Reply
#7
Joost Wrote:Fine, I was too lazy, and only checked one website. Regardless of that, DoEvents are bad, and I'll do my best avoiding them in my code as much as I can.

DoEvents are not necessarily bad, as long as you use them properly Tongue But it's all good, it's a matter of coding style and opinion.
Reply
#8
The only time I'd use a DoEvents is in a main loop.
Reply
#9
Even in that case, I'd stay away from it and use the Sleep function.
Reply
#10
Joost Wrote:Even in that case, I'd stay away from it and use the Sleep function.

How would one use the sleep function instead? Example? Also is there any benefits, because I'd be willing to convert all my sources from DoEvents if so.
Reply
#11
Nean Wrote:
Joost Wrote:Even in that case, I'd stay away from it and use the Sleep function.

How would one use the sleep function instead? Example? Also is there any benefits, because I'd be willing to convert all my sources from DoEvents if so.

Lol, the problem with sleep is that it does not allow you to do anything else, so it is basically useless. The reason you would use DoEvent is to allow users to still have some input while your code is looping. If that is what you are aiming for, stay as far away from Sleep as you can xD.
Reply
#12
http://www.geekinterview.com/question_details/38517

Here Ozzy stated that there are more proper way to do things and boasted that everyone should agree with him in a rather rude manner.
Reply
#13
Joost Wrote:http://www.geekinterview.com/question_details/38517

Out of curiosity, what would be your better alternative?
Reply
#14
Hilariously, Joke is right. DoEvents are a non-blocking way to use the "sleep" function in essence. The problems only occur when DoEvents is over used or misused. To properly use it is not easy, but in small apllications it has such a small effect on program flow. Mainly, if it is a GUI process, you should lock GUI, ect. There are many ways to stop it from being "evil" without the work of threading, which is sloppier than the devil to use in a small program.

This come from industry experience and over 10 years of programming with Visual Basic.
Reply
#15
DoEvents were the cause of the biggest bug I've ever found in my game. It is evil. You can use it but you should be careful. I've used to get a RTE 28 (Out of stack space). But the problem was that the bug was not on the line that the DoEvents were, it was just random, so really hard to find out the bug.
Be careful.
Reply
#16
From what I know, sleep stops the ENTIRE thread, not just the loop its in. Unless you multi thread VB, the main thread that everything is running through (including DirectInput and others) will be frozen as well.

For a game in Visual Basic, the MS form controls are just fine. In fact, even many [C, C++, C#, ect.] applications use polling of window objects for input. Directinput is helpful if you are using DirectX to form your GUI and not windows commands.
Reply
#17
I could see how this improved DoEvents could work, but I'm not getting any better FPS from it...I was expecting at least +100 FPS.
Reply
#18
Did my own benchmark and it's a nice improvement.

You're not gonna get over 9000 FPS just because of this, but it's a nice piece of code which you might as well use.

Thanks
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply
#19
I had problems using this. I forgot exactly what they were, though...I think it was a problem with walking or something?
Reply
#20
Mouse movements don't trigger the doevents like they should.
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)