Mirage Source

Free ORPG making software.
It is currently Thu Mar 28, 2024 9:45 pm

All times are UTC


Forum rules


Make sure your tutorials are kept up to date with the latest MS4 releases.



Post new topic Reply to topic  [ 1703 posts ]  Go to page 1, 2, 3, 4, 5 ... 69  Next
Author Message
PostPosted: Fri Jul 31, 2009 8:32 pm 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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 :)

_________________
Image


Top
 Profile  
 
PostPosted: Fri Jul 31, 2009 10:24 pm 
Offline
Knowledgeable

Joined: Sat Jul 08, 2006 8:24 am
Posts: 339
DoEvents are generally bad and tend to cause a lot of errors.


Top
 Profile  
 
PostPosted: Fri Jul 31, 2009 10:36 pm 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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..

_________________
Image


Top
 Profile  
 
PostPosted: Sat Aug 01, 2009 10:04 am 
Offline
Knowledgeable

Joined: Sat Jul 08, 2006 8:24 am
Posts: 339
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


Top
 Profile  
 
PostPosted: Sat Aug 01, 2009 3:00 pm 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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. :)

_________________
Image


Top
 Profile  
 
PostPosted: Sat Aug 01, 2009 5:25 pm 
Offline
Knowledgeable

Joined: Sat Jul 08, 2006 8:24 am
Posts: 339
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.


Top
 Profile  
 
PostPosted: Sat Aug 01, 2009 7:23 pm 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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 :P But it's all good, it's a matter of coding style and opinion.

_________________
Image


Top
 Profile  
 
PostPosted: Sun Aug 02, 2009 12:33 am 
Offline
Persistant Poster
User avatar

Joined: Thu Mar 29, 2007 10:30 pm
Posts: 1510
Location: Virginia, USA
Google Talk: hpmccloud@gmail.com
The only time I'd use a DoEvents is in a main loop.

_________________
Nean wrote:
Yes harold. Give it to me.

Image
Image


Top
 Profile  
 
PostPosted: Sun Aug 02, 2009 3:33 am 
Offline
Knowledgeable

Joined: Sat Jul 08, 2006 8:24 am
Posts: 339
Even in that case, I'd stay away from it and use the Sleep function.


Top
 Profile  
 
PostPosted: Sun Aug 02, 2009 3:36 am 
Offline
Persistant Poster
User avatar

Joined: Thu Jul 24, 2008 6:42 am
Posts: 703
Google Talk: infectiousbyte@gmail.com
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.

_________________
Image
GIAKEN wrote:
Since I'm into men, not women

GIAKEN wrote:
I can't take these huge penises anymore! All that's left is shame! And blood


Top
 Profile  
 
PostPosted: Sun Aug 02, 2009 4:12 am 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
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.

_________________
Image


Top
 Profile  
 
PostPosted: Sun Aug 02, 2009 8:39 pm 
Offline
Knowledgeable

Joined: Sat Jul 08, 2006 8:24 am
Posts: 339
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.


Top
 Profile  
 
PostPosted: Sun Aug 02, 2009 10:06 pm 
Offline
Knowledgeable
User avatar

Joined: Sun Nov 19, 2006 6:59 pm
Posts: 213
Joost wrote:
http://www.geekinterview.com/question_details/38517


Out of curiosity, what would be your better alternative?

_________________
Image


Top
 Profile  
 
PostPosted: Mon Aug 03, 2009 1:19 pm 
Offline
Pro

Joined: Mon May 29, 2006 2:58 pm
Posts: 370
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.

_________________
Image


Top
 Profile  
 
PostPosted: Mon Aug 03, 2009 5:08 pm 
Offline
Pro
User avatar

Joined: Mon May 29, 2006 3:26 pm
Posts: 493
Location: São Paulo, Brasil
Google Talk: blackagesbr@gmail.com
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.

_________________
http://www.blackages.com.br
Image
Dave wrote:
GameBoy wrote:
www.FreeMoney.com
I admit I clicked. I immediately closed upon realizing there was, in fact, no free money.
Robin wrote:
I love you and your computer.Marry me.


Top
 Profile  
 
PostPosted: Mon Aug 03, 2009 8:18 pm 
Offline
Banned
User avatar

Joined: Mon Jun 05, 2006 9:22 pm
Posts: 394
Location: USA
Cleaned. Also, can you post some examples of DirectInput or link to some articles instead of just calling people names and mentioning it offhand? Please, from now on, instead of just saying "MS Does this poorly" please cite exactly what you are talking about and offer up a viable solution to this bad way instead of raving on like a jerk.


Top
 Profile  
 
PostPosted: Mon Aug 03, 2009 8:21 pm 
Offline
Pro

Joined: Mon May 29, 2006 2:58 pm
Posts: 370
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.

_________________
Image


Top
 Profile  
 
PostPosted: Sun Aug 23, 2009 12:02 am 
Offline
Persistant Poster
User avatar

Joined: Thu Mar 29, 2007 10:30 pm
Posts: 1510
Location: Virginia, USA
Google Talk: hpmccloud@gmail.com
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.

_________________
Nean wrote:
Yes harold. Give it to me.

Image
Image


Top
 Profile  
 
PostPosted: Sat Sep 26, 2009 3:49 pm 
Offline
Submit-Happy
User avatar

Joined: Fri Jun 16, 2006 7:01 am
Posts: 2768
Location: Yorkshire, UK
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 <3

_________________
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?


Image


Top
 Profile  
 
PostPosted: Sun Sep 27, 2009 5:38 pm 
Offline
Persistant Poster
User avatar

Joined: Thu Mar 29, 2007 10:30 pm
Posts: 1510
Location: Virginia, USA
Google Talk: hpmccloud@gmail.com
I had problems using this. I forgot exactly what they were, though...I think it was a problem with walking or something?

_________________
Nean wrote:
Yes harold. Give it to me.

Image
Image


Top
 Profile  
 
PostPosted: Sun Sep 27, 2009 6:10 pm 
Offline
Submit-Happy
User avatar

Joined: Fri Jun 16, 2006 7:01 am
Posts: 2768
Location: Yorkshire, UK
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?


Image


Top
 Profile  
 
PostPosted: Wed Dec 01, 2021 9:24 am 
Offline
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 456192
http://audiobookkeeper.ruhttp://cottagenet.ruhttp://eyesvision.ruhttp://eyesvisions.comhttp://factoringfee.ruhttp://filmzones.ruhttp://gadwall.ruhttp://gaffertape.ruhttp://gageboard.ruhttp://gagrule.ruhttp://gallduct.ruhttp://galvanometric.ruhttp://gangforeman.ruhttp://gangwayplatform.ruhttp://garbagechute.ruhttp://gardeningleave.ruhttp://gascautery.ruhttp://gashbucket.ruhttp://gasreturn.ruhttp://gatedsweep.ruhttp://gaugemodel.ruhttp://gaussianfilter.ruhttp://gearpitchdiameter.ru
http://geartreating.ruhttp://generalizedanalysis.ruhttp://generalprovisions.ruhttp://geophysicalprobe.ruhttp://geriatricnurse.ruhttp://getintoaflap.ruhttp://getthebounce.ruhttp://habeascorpus.ruhttp://habituate.ruhttp://hackedbolt.ruhttp://hackworker.ruhttp://hadronicannihilation.ruhttp://haemagglutinin.ruhttp://hailsquall.ruhttp://hairysphere.ruhttp://halforderfringe.ruhttp://halfsiblings.ruhttp://hallofresidence.ruhttp://haltstate.ruhttp://handcoding.ruhttp://handportedhead.ruhttp://handradar.ruhttp://handsfreetelephone.ru
http://hangonpart.ruhttp://haphazardwinding.ruhttp://hardalloyteeth.ruhttp://hardasiron.ruhttp://hardenedconcrete.ruhttp://harmonicinteraction.ruhttp://hartlaubgoose.ruhttp://hatchholddown.ruhttp://haveafinetime.ruhttp://hazardousatmosphere.ruhttp://headregulator.ruhttp://heartofgold.ruhttp://heatageingresistance.ruhttp://heatinggas.ruhttp://heavydutymetalcutting.ruhttp://jacketedwall.ruhttp://japanesecedar.ruhttp://jibtypecrane.ruhttp://jobabandonment.ruhttp://jobstress.ruhttp://jogformation.ruhttp://jointcapsule.ruhttp://jointsealingmaterial.ru
http://journallubricator.ruhttp://juicecatcher.ruhttp://junctionofchannels.ruhttp://justiciablehomicide.ruhttp://juxtapositiontwin.ruhttp://kaposidisease.ruhttp://keepagoodoffing.ruhttp://keepsmthinhand.ruhttp://kentishglory.ruhttp://kerbweight.ruhttp://kerrrotation.ruhttp://keymanassurance.ruhttp://keyserum.ruhttp://kickplate.ruhttp://killthefattedcalf.ruhttp://kilowattsecond.ruhttp://kingweakfish.ruhttp://kinozones.ruhttp://kleinbottle.ruhttp://kneejoint.ruhttp://knifesethouse.ruhttp://knockonatom.ruhttp://knowledgestate.ru
http://kondoferromagnet.ruhttp://labeledgraph.ruhttp://laborracket.ruhttp://labourearnings.ruhttp://labourleasing.ruhttp://laburnumtree.ruhttp://lacingcourse.ruhttp://lacrimalpoint.ruhttp://lactogenicfactor.ruhttp://lacunarycoefficient.ruhttp://ladletreatediron.ruhttp://laggingload.ruhttp://laissezaller.ruhttp://lambdatransition.ruhttp://laminatedmaterial.ruhttp://lammasshoot.ruhttp://lamphouse.ruhttp://lancecorporal.ruhttp://lancingdie.ruhttp://landingdoor.ruhttp://landmarksensor.ruhttp://landreform.ruhttp://landuseratio.ru
http://languagelaboratory.ruhttp://largeheart.ruhttp://lasercalibration.ruhttp://laserlens.ruhttp://laserpulse.ruhttp://laterevent.ruhttp://latrinesergeant.ruhttp://layabout.ruhttp://leadcoating.ruhttp://leadingfirm.ruhttp://learningcurve.ruhttp://leaveword.ruhttp://machinesensible.ruhttp://magneticequator.ruhttp://magnetotelluricfield.ruhttp://mailinghouse.ruhttp://majorconcern.ruhttp://mammasdarling.ruhttp://managerialstaff.ruhttp://manipulatinghand.ruhttp://manualchoke.ruhttp://medinfobooks.ruhttp://mp3lists.ru
http://nameresolution.ruhttp://naphtheneseries.ruhttp://narrowmouthed.ruhttp://nationalcensus.ruhttp://naturalfunctor.ruhttp://navelseed.ruhttp://neatplaster.ruhttp://necroticcaries.ruhttp://negativefibration.ruhttp://neighbouringrights.ruhttp://objectmodule.ruhttp://observationballoon.ruhttp://obstructivepatent.ruhttp://oceanmining.ruhttp://octupolephonon.ruhttp://offlinesystem.ruhttp://offsetholder.ruhttp://olibanumresinoid.ruhttp://onesticket.ruhttp://packedspheres.ruhttp://pagingterminal.ruhttp://palatinebones.ruhttp://palmberry.ru
http://papercoating.ruhttp://paraconvexgroup.ruhttp://parasolmonoplane.ruhttp://parkingbrake.ruhttp://partfamily.ruhttp://partialmajorant.ruhttp://quadrupleworm.ruhttp://qualitybooster.ruhttp://quasimoney.ruhttp://quenchedspark.ruhttp://quodrecuperet.ruhttp://rabbetledge.ruhttp://radialchaser.ruhttp://radiationestimator.ruhttp://railwaybridge.ruhttp://randomcoloration.ruhttp://rapidgrowth.ruhttp://rattlesnakemaster.ruhttp://reachthroughregion.ruhttp://readingmagnifier.ruhttp://rearchain.ruhttp://recessioncone.ruhttp://recordedassignment.ru
http://rectifiersubstation.ruhttp://redemptionvalue.ruhttp://reducingflange.ruhttp://referenceantigen.ruhttp://regeneratedprotein.ruhttp://reinvestmentplan.ruhttp://safedrilling.ruhttp://sagprofile.ruhttp://salestypelease.ruhttp://samplinginterval.ruhttp://satellitehydrology.ruhttp://scarcecommodity.ruhttp://scrapermat.ruhttp://screwingunit.ruhttp://seawaterpump.ruhttp://secondaryblock.ruhttp://secularclergy.ruhttp://seismicefficiency.ruhttp://selectivediffuser.ruинфоhttp://semifinishmachining.ruhttp://spicetrade.ruhttp://spysale.ru
http://stungun.ruhttp://tacticaldiameter.ruhttp://tailstockcenter.ruhttp://tamecurve.ruhttp://tapecorrection.ruhttp://tappingchuck.rutaskreasoning.ruhttp://technicalgrade.ruhttp://telangiectaticlipoma.ruhttp://telescopicdamper.ruсайтhttp://temperedmeasure.ruhttp://tenementbuilding.rutuchkashttp://ultramaficrock.ruhttp://ultraviolettesting.ru


Top
 Profile  
 
PostPosted: Fri Jan 07, 2022 11:02 pm 
Offline
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 456192
Econ


Top
 Profile  
 
PostPosted: Fri Jan 07, 2022 11:03 pm 
Offline
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 456192
120.1


Top
 Profile  
 
PostPosted: Fri Jan 07, 2022 11:04 pm 
Offline
Mirage Source Lover

Joined: Sun Jul 04, 2021 4:04 am
Posts: 456192
Bett


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1703 posts ]  Go to page 1, 2, 3, 4, 5 ... 69  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group