I don't understand why one would use the DoEvents function.
How does it affect the application?
Thanks.
Dan.
Printable View
I don't understand why one would use the DoEvents function.
How does it affect the application?
Thanks.
Dan.
DoEvents lets the OS 'work' for a fraction of a second before returning to the procedure.
I use it all the time
Lets say you have a long process that needs to run but your program is also monitoring a COMM port for incomming data, with out a doevents it the process loop (or multithreading the app) the incomming data will never be detected
It is also good for a stop button or anything to stop a certain sub or function.
Code:Private Sub cmdstop_Click()
Do
DoEvents
Loop
End Sub
Doevents will allow YOUR applications events to get fired.
DoEvents can prevent the OS from freezing by allowing it to process other events in it's queue
Don't be so sure on that, Sam told me that it only concerns your applications events
This is a good skeleton to understand DOEVENTS.Quote:
Originally posted by Matthew Gates
It is also good for a stop button or anything to stop a certain sub or function.
Code:Private Sub cmdstop_Click()
Do
DoEvents
Loop
End Sub
try something like thisYour App and the IDE will freeze for 10 seconds, but other processes will continue to function as normal. (unless you've been messin with thread priorities) All doevents does is check your own threads message queue and process any events such as command clicks, repainting and moving etc. without doevents your App apears to be frozen, but other processes keep going.Code:Private Sub Command1_Click()
Dim x As Long
x = Timer
Do Until Timer > (x + 10)
Loop
MsgBox "Hello"
End Sub
Hello Sam
Danab, start a new project and drop a command button on the form.
Run the program. Click on the form and when you want to stop click on the command button.Code:Option Explicit
Private b_Cancel As Boolean
Private Sub Command1_Click()
b_Cancel = True
End Sub
Private Sub Form_Click()
b_Cancel = False
Do While Not b_Cancel
Me.BackColor = QBColor(Rnd * 10)
DoEvents
Loop
End Sub
[Edited by Nitro on 07-27-2000 at 09:31 PM]
Hmm, what do you think guys? Isn't this a good progamming habit?
Code:Do
'Code
doevents
Loop While Forms.count
not really, it screws up unloading the form.
What do you mean "skeleton"?Quote:
Originally posted by Nitro
This is a good skeleton to understand DOEVENTS.
But actually, I like it because, well...explanation:
So making it as a stop button will stop any events that are happening because it is doing the current code, but there is nothing to do actually so it just hangs there :p. Get what I mean?Code:Do 'Do the process
DoEvents 'Just do nothing
Loop 'Loop it
You didn't mean me right?= because it should prevent screwing up when unloading all formsQuote:
not really, it screws up unloading the form.
Matthew. DoEvents may seem like it does nothing, but in reality, it pauses execution for a fraction of a second so the OS can process other tasks.
IN reality Doevents allows YOUR events to get fired, not allowing Other tasks getting processed, and that fraction of a second depends on what event's you get fired