» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Multiple DoEvents

It seems that having more than one DoEvents will really slow down the loop a lot.

But what is DoEvents now exactly?
Well, DoEvents gives Windows some time for it's other threads (other running services/applications). Without any DoEvents, your application will seem to "hang", since it does not give Windows time to update anything else.

Code:
Public Sub TestOne() Dim I As Long Do While I < 10000 DoEvents I = I + 1 DoEvents Loop End Sub Public Sub TestTwo() Dim I As Long Do While I < 10000 I = I + 1 DoEvents Loop End Sub

2x DoEvents % faster than 1x DoEvents 2x DoEvents (sec) 1x DoEvents (sec)
111.3% 1.225502 0.579911
95.2% 1.146837 0.587599
122.8% 1.154756 0.518364
72.5% 0.981416 0.568837
77.7% 1.141622 0.642500


User contributed notes:

Author: Mike () Date: 04:06 06/06/2004
DoEvents gives focus to the rest of the code to be run while the loop is going. if you call it more then once its like running your entire program through twice in practically a millisecond. You probably knew this already but in my opinion that test was pretty much pointless.

Author: Almar Joling () Date: 10:06 09/06/2004
Lots of people put multiple DoEvents through their game, hence the reasonI just put it up here for demonstration. :)

Author: Sr. Guapo () Date: 02:06 17/06/2004
It is also nice to have the exact numbers, though... Obviously it would be slower to call anything more than once in a loop, this test just shows how much slower.

Author: Jamie Hurst (JamieAlexanderHurst at hotmail dot com) Date: 18:05 26/05/2005
I use just 1 Doevent for every x increments in a loop. For example

Do Until I& => 10000
I& = I& + 1
P% = P% + 1
If P% = 40 Then
Doevents
P% = 0
End If
Loop


Jamie

Author: Almar () Date: 18:05 26/05/2005
You could use Mod on that one. I think the If..Then might be slower. Not sure.

Author: Tom (hurendo_kun at hotmail dot com) Date: 15:05 31/05/2005
Yeah, but he still needs the conditional to process his DoEvents.

P = (P + 1) Mod 40
If P = 40 Then DoEvents

That whole loop structure looks kind of messy to me, though.

Author: Tom (hurendo_kun at hotmail dot com) Date: 16:06 08/06/2005
Whoops, it would be "If P = 0". Sorry.

Author: TheShau (shauros at walla dot com) Date: 15:08 04/08/2006
Sometimes in "loop in a loop" situations you'll need to carefully decide where the DoEvents should go. Like in multi-buffered transfer of large data. But generally there should never be more then one per loop structure.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: