Sleep waits for occurrence of a message and invokes an event sub.
Sleep [n]
n:iexp
Sleep handles all pending messages for the application and switches control to the operating-environment kernel. Control returns to your application as soon as all other applications in the environment have had a chance to respond to pending events. This doesn't cause the current application to give up the focus, but it does enable background events to be processed.
The main message loop of a GFA-BASIC 32 application should always use Sleep, never DoEvents. Sleep is especially created to handle the OLE based user interface.
Sleep can be used with a parameter, which specifies the number of milliseconds to wait before returning to the application. Sleep 0 returns immediately to the application and behaves more like DoEvents.
Note: Sleep waits for a message, but in the IDE it returns after a short delay. This is due to a WM_TIMER message for the IDE itself, which allows intercepting the Ctrl-Break keys to stop the program. Without a WM_TIMER Sleep could wait forever, especially when all windows have been closed. A compiled program doesn't behave like this.
OpenW 1
PrintWrap = 1
PrintScroll = 1
Do
Sleep
'DoEvents
// to see the difference with DoEvents
// remove the comment
Print "*";
Until MouseK = 2
// Using Until Win_1 Is Nothing does not work here
// due to the inclusion of the Print "*"; line inside
// the loop
By using DoEvents instead of Sleep, all simultaneous running programs (also server activities, printer spooler, etc.) will slow down. A loop with DoEvents prevents energy saving of a notebook. DoEvents was created only to use during long arithmetical calculation operations.
GetEvent and Sleep are alike. Both wait for a message before going on. Sleep handles all pending messages, where GetEvent only handles one message and uses the Menu() array to store messages. Sleep doesn't use the Menu() array at all.
When porting a GFA-BASIC 16 program you shouldn't use DoEvents or Sleep, but GetEvent or PeekEvent. By using GetEvent or PeekEvent you can get problems, when you use Ocx controls in your program simultaneously.
As a rule: Don’t mix the Menu() array handling and Ocx controls. Use GetEvent/PeekEvent only in programs, that use the Menu() array. A program that uses Ocxs has to use Sleep (and DoEvents).
{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}