|
-
November 1st, 2002, 04:47 PM
#1
DoEvents
would someone educate me about DoEvents ?
I am using DoEvents in the following function under the impression that When it waits for the time to elapse , let the OS do something else.
But my Supervisor has told me NEVER user DoEvents as it will help up resources. 
I thought DoEvents is to release the resources.
could you please advise me on this ?
Code:
Public Sub Delay(NumberOfSeconds As Integer)
Dim start As Single
Dim Kounter As Integer
On Error Resume Next
start = Timer
Kounter = 0
Do While Timer < start + NumberOfSeconds
DoEvents
Loop
End Sub
-
November 1st, 2002, 06:34 PM
#2
DoEvents yields execution so that the operating system can process other events. In other words, it prevents your program or function from locking up the system resources. It is necessary if you want to have a button allowing the user to cancel a process.
DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.
This doesn't cause the current application to give up the focus, but it does enable background events to be processed.
There are dangers however. Any time you temporarily yield the processor within an event procedure, make sure theprocedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results. In addition, do not use DoEvents if other applications could possibly interact with your procedure in unforeseen ways during the time you have yielded control.
The VB help files have more information. In general, it is not something to be avoided, but rather should be used to prevent your program from using up all the system's resources.
-
November 1st, 2002, 10:51 PM
#3
Code:
Do While Timer < start + NumberOfSeconds
DoEvents
Loop
In that loop, the DoEvents will be called, then the form will process events (if any) like mouse move and button click, but after some milliseconds, the loop will continue. The Loop will return to the DO and execute the condition (Timer < start + NumberOfSeconds), those last two operation take ressources. Since the loop might be executed many times per seconds, you'll end with a cpu usage 100% maybe. That's because the processor do not end any operations for enough time. When it is not executing the loop, it is processing the form events.
To arrange that, you may use a TIMER control, or, in you Do Loop, you could use a Sleep() , for example
Code:
Do While Timer < start + NumberOfSeconds
Sleep(1000) 'Sleep API, make the application sleep for 1 second
DoEvents
Loop
Using a sleep with help the processor ressource, but events will only be processed once per seconds 
JeffB
-
November 2nd, 2002, 03:30 AM
#4
I think u must supervise ur supervisor,
DoEvents helps to control the CPU USAGE by ur application
DoEvents is usefull so that ur application does not lock up resources
Last edited by Nilesh_S_Gokhal; November 2nd, 2002 at 03:33 AM.
Nilesh
Pune, Maharashtra
India
-
November 2nd, 2002, 02:21 PM
#5
I use the DoEvents when I want to show a progressbar (thats on a seperate form) for any long actions I am doing. If I do not add the DoEvents command after each reference to the progressbar, the progressbar will not appear to be doing anything.
Example:
frmProgress.pgbProgress.Max = (Value)
frmProgress.Show
DoEvents
(I have to add the DoEvents to make the frmProgress to appear)
Long data manipulation event occuring ................
frmProgress.pgbActionProgress.Value = (value)
DoEvents
(I have to add the DoEvents so the user can see the pgbProgress bar advancing)
This is just a little example of the DoEvents keyword.
Hope it helps you understand it a little better.
-
November 2nd, 2002, 04:02 PM
#6
DoEvents solved a problem for me a week ago.
I am running a simulation program in a very havy 3D loop
for z = 0 to ..
for y = 0 to ..
for x = 0 to ...
...
... LOT of calculations
...
next
next
next
The problem with this code is that I could not interact with my application window. The program did not handle/process any events. TOO BUSY.
I modified the code as follows:
for z = 0 to ..
for y = 0 to ..
for x = 0 to ...
DoEvents
...
... LOT of calculations
...
next
next
next
Now the program listens to any control event I, as a user, generate. Before, the application was frozen completely.
When a heavy loop in your code prevents other events to be processed, the DoEvent is very usefull (when you put it at the right place - as deep as possible.
-
November 2nd, 2002, 11:21 PM
#7
DoEvents is usefull since Threading in VB is not well supported. But remember that your loop MUST END. If you use a loop with DoEvents to show time on a label of your form, you'll need the loop to always work to show time each seconds. This will lock ressources and cpu usage, so not a gooc choice.
Use DoEvents when you do any hard loop (to make sure the program do not freeze or to show echo on your form) and when that loop finish. For example, copying a lot of files and showing the progress on a progress bar. The ressources and cpu usage will be at top usage, but this will eventually end.
If you need something that will always be "looping", use a timer control 
JeffB
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
|