Solved

Do Events

Posted on 2002-03-18
6
176 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 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