Normally the programmer expects that Windows (and Visual Basic) permanently traces all events and reacts to them. On the other hand, a lot of time leaves on any calculations or sorting of the data. However thus not always it is desirable to forbid for application Visual Basic to react to events. If there is a procedure in which a lot of time will be occupied by any calculations, the mechanism of tracing of a state of an operating system and reaction to any events is necessary.
The function which is carrying out of the such task, carries title DoEvents. In whatever place of the program there was a given operator, it signals Visual Basic that control is transferred an operating system for handling of all events. (Windows saves sequence of approach of events in queue, and also pushings of keys in queue SendKeys.) DoEvents it is impossible to use in event procedure which is caused some times. For example, event procedure Click can be caused once again mouse click. If to forget about it, it is possible to organize easily in the program an infinite loop.
The cycle working only in case of absence of any events, carries the wait loop name. Such cycles form in the specialized procedure Sub bearing name Main, and connected to any software module. In each project there is only one procedure Main. Its format following:
Sub Main ()
Do While DoEvents ()
' the Code working in the course of a wait loop
Loop
End Sub
Further it is necessary to make so that the unit with Main booted the first. For this purpose it is necessary to select from the menu point Project|Project Properties and then page General. On this page to select and open list StartUp Object and to select Sub Main instead of the form. After procedure Main is specified load. Visual Basic will not load automatically any form. For this purpose now it is necessary to write the program code, using, served words Load and Show.
The simple example of a wait loop is more low resulted. It is necessary to create the new project and to add to it software module. Further in it global variable Counter of type long integer is declared:
Public Counter As Long
Now we add some code lines to procedure Main:
Sub Main ()
Fonnl. Show
Do While DoEventsf)
Counter = Counter + 1
loop
End Sub
In end we install Sub Main as load module, and we add procedure Form_Click:
Sub Form_Click ()
Print Counter
End Sub
After start of such program the counter will increase each time after click in the form. The reason consists that during a latent period (when the user does not click the mouse) Visual Basic passes to procedure Main and adds 1 to the counter. As Counter is global variable, its value is saved between procedure calls.
Function DoEvents actually returns number of the forms loaded by application at a present situation. The wait loop stops, when all forms are preempted. (Or Visual Basic operator End meets.)
Other general usage of function DoEvents in function consists in start of the big calculations on time. Wait loop setting allows Visual Basic to react to events at the moment of calculations. The small extra time thus with interest pays off.