Application.DoEvents

From Xojo Documentation

(Redirected from DoEvents)
Method

Application.DoEvents([milliseconds as Integer])

New in 5.5

Supported for all project types and targets.

Yields time back to your app when in loops. Intended for console applications in which there is no main event loop.

Notes

The optional parameter specifies the amount of time you want the currently executing thread to sleep. DoEvents can cause a Thread to sleep with a resolution greater than 16 milliseconds. If all threads are sleeping, then your app yields back time to the system. This allows you to write applications that don't use up to 100% of the CPU during tight loops.

Specifying zero milliseconds causes the next waiting thread to execute. A negative value specifies no sleep. The default is -1.

In Console Apps

By design, console applications do not have a main event loop. It implies that classes relying on such event loop will not work as expected, like Timers or sockets. See the equivalent method ConsoleApplication.DoEvents to see how to use Timers and sockets in a console application.

In Desktop Apps

Using DoEvents in a GUI application will likely cause instability. In effect, you would be placing a main event loop inside the “real” main event loop. You should consider using threads to handle lengthy operations rather than placing them in the main thread and calling DoEvents to maintain the interface.

If the current method is running inside a Thread, DoEvents will yield to the main thread instead of running one iteration of the event loop inside the thread, causing confusion.

If you use DoEvents inside a loop, then you cannot use the UserCancelled function to detect whether the user pressed the Esc key (Windows or Linux) or Command-period (on Macintosh) to break out of the loop. Since DoEvents keeps the user interface responsive while the loop is running, you can add a button to the user interface that the user can click to stop the loop.