Do You DoEvents?
Lots of programmers don't even know about Visual Basic's DoEvents function.
This is not surprising because few Visual Basic programs need it. DoEvents
returns control to the operating system temporarily, allowing it to process
other events that may have occurred. In my experience, the only time DoEvents
is needed is when a program has code that takes a long time to execute, such as
certain complex mathematical calculations. By calling DoEvents at strategic
locations in your code you can improve program responsiveness.
To see what I mean, create a Standard EXE project in Visual Basic and place one
CommandButton and one TextBox on the form. Then, put the following code in the
Command Button's Click event procedure:
Private Sub Command1_Click()
Dim i As Long, j As Long
For i = 1 To 100
Text1.Text = i
For j = 1 To 100000
Next
Next
Text1.Text = "Done"
End Sub
You can see that the code has one loop within another, loops that will take a
few seconds to complete (You may want to adjust the value that the inner loop
counts to depending on the speed of your system). Each time the outer loop
iterates, the current value of i is displayed in the text box. When the loops
are finished, "done" is displayed.
What actually happens when you run the program, however, is that the text box
does not change until "done" is displayed. The problem is that the system was
so busy executing the loops that the requests to display i in the text box got
stalled in Windows queue. When the loops were finished, all these requests were
processed, too quickly for you to see on the screen.
Now, place a call to DoEvents in the code, just after the Text1.Text = i
statement. When you run the program you will see that the text box "counts up"
the values of i, just as you would expect. Calling DoEvents frees up the system
to process the request, then returns control to the Visual Basic program.
DoEvents is not without potential problems. For example, if you call DoEvents
from a procedure you must be sure that the same procedure cannot be called
again before execution returns from the first call - otherwise unpredictable
results may occur. Likewise, DoEvents should be avoided if other applications
could interact with the procedure in unforeseen ways. Use of a Timer control or
isolation of long-running code in an ActiveX component are two other approaches
to improving program responsiveness.
----- Original Message -----
From: HouseDad
To: [EMAIL PROTECTED]
Sent: Tuesday, December 07, 2004 6:55 AM
Subject: [vbhelp] TEXTBOX UPDATE
Hello all, it's your annoying groupie again.
I have a problem with the textbox being updated properly. What
I mean is, for instance, a button is pressed to begin a process.
When this button is pressed I change the cursor, send progress info
such as "Working on Item#...", and set the visible property of the
button control to false.
The problem is none of these things are visibly apparent until
the program is done processing data. It looks like the program locks
up until it's done, then all the things that should have happened
when the button was first pressed happen at once when it is finished.
So far the only way I have figured out to update the form is to
pop up msgbox's, which is annoying and slows the process.
Any ideas? Would an extra form.show command help? I can't make
a progress bar either because it doesn't show progress until it's
already done.
[C]
'// =======================================================
Rules : http://ReliableAnswers.com/List/Rules.asp
Home : http://groups.yahoo.com/group/vbHelp/
=======================================================
Post : [EMAIL PROTECTED]
Join : [EMAIL PROTECTED]
Leave : [EMAIL PROTECTED]
'// =======================================================
Yahoo! Groups Sponsor
ADVERTISEMENT
------------------------------------------------------------------------------
Yahoo! Groups Links
a.. To visit your group on the web, go to:
http://groups.yahoo.com/group/vbhelp/
b.. To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
[Non-text portions of this message have been removed]
------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/k7folB/TM
--------------------------------------------------------------------~->
'// =======================================================
Rules : http://ReliableAnswers.com/List/Rules.asp
Home : http://groups.yahoo.com/group/vbHelp/
=======================================================
Post : [EMAIL PROTECTED]
Join : [EMAIL PROTECTED]
Leave : [EMAIL PROTECTED]
'// =======================================================
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/vbhelp/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/