Microsoft KB Archive/136202

= Microsoft Knowledge Base =

How to Pass a Callback Function to the SetTimer Windows API
Last reviewed: July 25, 1996

Article ID: Q136202

The information in this article applies to:


 * Microsoft Visual Test for Windows 95 and Windows NT, version 4.0

SUMMARY
Some Windows API functions take a callback function as a parameter. These API functions fire the supplied callback function at times determined by the function. This article shows by example how to pass a callback function to the SetTimer Windows API function.

MORE INFORMATION
The SetTimer function fires a TestBasic Sub procedure or callback function at a specified time interval when the TestBasic script is idle. If the TestBasic script is not idle, the SetTimer function fires a TestBasic Sub procedure as soon as the TestBasic script enters an idle state after the specified time interval.

A TestBasic script enters an idle state when processing many of its keywords or by calling Sleep. TestBasic and Windows API functions that enter an idle state are listed later in this article.

When SetTimer fires the given callback function, the TestBasic script pauses, runs the code in the callback function up to the function's End [Sub | Function] statement, and then continues execution where the Test script left off.

Sample Code
The following example code shows how to pass a callback function (TimerProc) to SetTimer, and it shows SetTimer calling the TimerProc callback function. A status box shows what is currently processing, the TimerProc callback function or the main script.

'$Include 'Declares'

Declare Sub TimerProc( hwnd&, msg&, id&, sysTime&)

Sub TimerProc ( hwnd&, msg&, id&, sysTime&) StatusBox "TimerProc is currently processing." + Chr$(13) + Chr$(10) + _ "[" + Time$ + "]" Sleep 2 End Sub If SetTimer( NULL, 0, 4 * 1000, VarPtr( TimerProc ) ) = 0 Then

' Failed to get a timer. End End If

While True

StatusBox "Main script is currently processing." + Chr$(13) + _ Chr$(10) + "[" + Time$ + "]" Sleep .25 Wend

Functions That May Enter an Idle State

 * Any function that takes a timeout as a parameter may enter an idle state.
 * If a function fails to accomplish its task during the first attempt, it enters an idle state, and then makes another attempt.
 * The TestBasic Sleep statement enters an idle state.
 * The Windows API functions GetMessage, PeekMessage, and Sleep cause a Windows-based application to enter an idle state.