Solved

Do Events

Posted on 2002-03-18
6
177 Views
Last Modified: 2013-11-25
Hello Experts:

Just a simple question.  Can someone explain what the 'Do Events' syntax performs?  Haven't been able to find and info on this.

Thanks
0
Comment
Question by:zdpl0a
6 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6878764
Is it DoEvents.. A command which let OS to handle other process/operation while you still processing. Sometime we use it to prevent the Interface from being 'hang'. Try to find more documentation on MSDN. Cheers
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6878818
Try F1 (HELP) while your cursor sits on "DoEvents" in a VB code window...
0
 
LVL 1

Accepted Solution

by:
RHuebner earned 5 total points
ID: 6878876
DoEvents command causes your process to yield to the next process in line. This command is used when you are using an out-of-process component (i.e. web browser) and are waiting for it to finish a command to prevent the preception that your program is hung and to keep two processes in sync.

Example 1 'Keeping processes in sync
ComponentA.LoadSomething          'Method of out of process
Do Until ComponentA.Ready = True 'Wait until component is ready
  DoEvents
Loop                             'Continue the Loop
ComponentA.DoSomething

Example 2 'Preventing the precention of being hung
For I = 1 to 1000000
 'Some commands
  DoEvents  'Allows user to do other things while waiting
Next

Hope this explains the command a little more. for the most part you shouldn't need to use it much.

Good Luck,
RH.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

Expert Comment

by:mdougan
ID: 6879853
Well, like RHuebner says, the currently active code will have the priority when running, and DoEvents is a way to allow other activity to occure that would normally have to wait until the running code is finished.  But I disagree about the out-of-process stuff.  I'm sure it will affect out-of-process stuff, but it is more commonly used to give some processing time to in-process stuff.

One example, let's say that you have a progress bar on your screen and you have code that goes to the database, gets a few thousand records and then loop through the recordset putting them into a table.  At the bottom of the loop, you update the progress bar to show the percent complete.

It's possible that even though you updated the progress bar's value, the change wont show up on the screen because the processor is so busy inside of your loop, that it doesn't have time to process the Paint Event that was sent to the progress bar to repaint and show the progress.  That Paint Event is sitting in the Windows Event Queue.

Doevents puts the current code on hold long enough to clear out the Windows Event Queue.  As soon as the Windows Event Queue is empty, it will return control to your active code.

The only time DoEvents is dangerous is if you have some other background process that is continually filling up the Windows Event Queue. If so, then when you issue a DoEvents, you might not ever come back from that call!

So, to finish with the example, if you put a DoEvents after the instruction that updated the value for the Progress Bar, then it would probably have enough time to refresh the bar after every loop.

In general, if you have a processing loop that will go one for some time, it's a good idea to include one of these in the loop, however, it can slow down your processing.
0
 
LVL 1

Expert Comment

by:RHuebner
ID: 6884550
The out-of-process comment come from past experence with the web-browser component, when having to wait for the component to finish loading a page before doing something with the document displayed you would need to use doevents to get the web-browser control to finish loading otherwise you could end up with some strange errors.

I must agree you should seldom use the doevents command cause you have no control over when your process gets to run again.

RH.
0
 

Author Comment

by:zdpl0a
ID: 6897090
Thanks for the input from everyone and the examples.. Answered my question
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question