|
-
Jun 16th, 2004, 03:38 AM
#1
Thread Starter
Lively Member
DoEvents
what extactly does a DoEvents do? does it pause the program so it wont lag?
-
Jun 16th, 2004, 03:42 AM
#2
So Unbanned
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').
-
Jun 16th, 2004, 03:50 AM
#3
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
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jun 16th, 2004, 04:28 AM
#4
your computer is your friend and not to include doevents is like not letting it breathe,
doevents lets your computer breathe
-
Jun 16th, 2004, 05:08 AM
#5
Frenzied Member
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
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Jun 16th, 2004, 07:42 AM
#6
Thread Starter
Lively Member
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?
-
Jun 16th, 2004, 07:47 AM
#7
Frenzied Member
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
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Jun 16th, 2004, 08:13 AM
#8
Frenzied Member
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.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
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
|