MsgWait timer for VB and VC++ applications
Sometimes you may want to delay for a specified time in you VB (or VC++) application or cause your application to sleep momentarily while waiting for some event to occur. The MsgWait sample code below is a subroutine that waits for a specified number of milliseconds. The MsgWait routine is based on the WinAPI MsgWaitForMultipleObjects. This allows your application to sleep until time has expired, and momentarily wake up to process events immediately.
First, we will compare three different loop methods using VB. The following Do..Loop causes your application to "hog" the CPU. If you observe CPU Usage while this loop occurs, you will see 100%. Events are processed immediately, but since the application is using the CPU more, the process that is sending events does not get as much CPU time, so event throughput is reduced.
Do DoEvents Loop Until g_CycleComplete This Do..Loop uses the WinAPI Sleep function. This considerably reduces the CPU Usage, but events will not be processed as quickly. In fact, no events will be processed during the Sleep time. 10 ms is the smallest interval that can be used. Do Sleep 10 DoEvents Loop Until g_CycleComplete By using MsgWait, your application will not cause the CPU Usage to be 100%. If an event occurs, it will be processed immediately. This is more efficient for processing events, since your application will sleep until one or more events occur, process the events, then sleep again. Do MsgWait 10 Loop Until g_CycleComplete Also, VB does not have a delay timer, so MsgWait can be used for this purpose. For example: ExecuteMyFunc1 MsgWait 1000 ExecuteMyFunc2 The table below shows a comparison between the different loop methods. The test was performed by running a SPEL+ task that executes SPELCom_Event in a loop with no delays. As you can see, the MsgWait method allows the greatest event throughput with low CPU usage.
| ||||||||||||

