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

Notes

To 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

  • 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.


Additional reference words: 4.00 Win32
KBCategory: kbprg kbcode kbwebcontent
KBSubCategory:


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 25, 1996
©1997 Microsoft Corporation. All rights reserved. Legal Notices.