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:
SUMMARYSome 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 INFORMATIONThe 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 CodeThe 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. EndEnd If While True StatusBox "Main script is currently processing." + Chr$(13) + _
Chr$(10) + "[" + Time$ + "]"
Sleep .25
Wend
NotesTo stop the sample code from running, double-click Break on the Test menu. Sleep is necessary in the While True loop. Without it, the Test script would never enter an idle state, so the callback function would never be fired.
Functions That May Enter an Idle State
|
|
Additional reference words: 4.00 Win32
©1997 Microsoft Corporation. All rights reserved. Legal Notices. |