what extactly does a DoEvents do? does it pause the program so it wont lag?
Printable View
what extactly does a DoEvents do? does it pause the program so it wont lag?
It pauses the execution at the DoEvents clause and processes other code on the stack. While 'moving' code after the DoEvents to the top of the stack(if you imagine it as first in first out, FIFO, so code after it would be 'last in, last out').
In other words it gives running back to windoze and lets windoze update the forms etc. instead of hanging the app (ctrl+alt+del -> status "not responding").
So if you do processor intensive programming loop, put an update on the screen for the user and use doevent to update it/keep your app open.
Vince :)
your computer is your friend and not to include doevents is like not letting it breathe,
doevents lets your computer breathe :)
yeah its kinda used when ur form is doing a big task, which will take a few seconds to do, so it doesnt "hang" and look like its crashed
so when i put doevents in a piece of code, does it pause that part? i dont really get it, can someone give me an example?
ok for an example i have an application which runs a few SQL quiries and display in a grid, to show the results in the first part, while the next SQL command runs its best to put the do events in, otherwise you will probably have to wait till the two SQL commands have completed and are then displayed, doevents is more for what the form shows
DoEvents example:
But a button a form, and put this code in the CLICK event of the button.
VB Code:
Private Sub Command1_Click() Dim i As Long Dim i2 As Long Command1.Enabled = False For i = 1 To 50000 For i2 = 1 To 10000 Next Next MsgBox "Done" Command1.Enabled = True End Sub
Run the app, click on the button.
Try and move the form - you can't until the loops have finished and you have dismissed the msgbox. This is because the PC is concentrately solely on the loops in the code and is not responding to other events (ie - you clicking and dragging the form with the mouse) until it has finished
Now edit the code slightly to:
VB Code:
Private Sub Command1_Click() Dim i As Long Dim i2 As Long Command1.Enabled = False For i = 1 To 50000 [b]DoEvents[/b] For i2 = 1 To 10000 Next Next MsgBox "Done" Command1.Enabled = True End Sub
Now when you click the button you will notice it takes longer to get to the msgbox - however this time you CAN drag the form around the screen because you have given Windows the opportunity to deal with any other events via the DoEvents command.
So, in essence, if you have a processor-intensive loop going on consider putting a DoEvents in to stop the program appearing to be "not responding" and allow the user to interact with it in other ways while the loop is running.
However - be warned - because other events (eg button clicks etc) WILL fire while the loop is running you have to make sure you deal with it appropiately - in my example I disabled the button while the loop was running to stop the user pressing it again.. just because they can.. until it had finished the loop.