SetTimer Function

Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Platforms

Description & Usage

SetTimer creates a timer that triggers an event after so many milliseconds have elapsed. The timer continues triggering events between intervals until it is removed using KillTimer. The timer can be configured to either send a WM_TIMER message to the owning window or call a callback function whenever the time-out period elapsed.

Return Value

If successful, the function returns a unique integer identifying the created timer. If an error occured, the function returns 0 (call GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

hWnd
A handle to the window that will own the timer. To make the timer unassociated with any window, set this to 0.
nIDEvent
A nonzero value that will be used to identify the timer. If hWnd is 0, this parameter is ignored.
uElapse
The time-out period, in milliseconds, to elapse between timer notifications.
lpTimerFunc
A pointer to the TimerProc callback function to call whenever the time-out period elapses. If this is 0, a WM_TIMER message is instead sent to the window specified by hWnd when the time-out period elapses.

Example

Display the current time in text box control Text1. The time is updated twice every second, and the time is formatted according to the current locale's settings. To use this example, place a text edit box named Text1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type SYSTEMTIME
	wYear As Integer
	wMonth As Integer
	wDayOfWeek As Integer
	wDay As Integer
	wHour As Integer
	wMinute As Integer
	wSecond As Integer
	wMilliseconds As Integer
End Type
Public Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent _
	As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public Declare Function GetTimeFormat Lib "kernel32.dll" Alias "GetTimeFormatA" (ByVal _
	Locale As Long, ByVal dwFlags As Long, lpTime As SYSTEMTIME, ByVal lpFormat As Any, _
	ByVal lpTimeStr As String, ByVal cchTime As Long) As Long

' *** Place the following code inside a module. ***

' The following function will execute twice every second.  It retrieves
' the current time and displays it according to the current locale's formatting preferences.
Public Sub TimerProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, _
		ByVal dwTime As Long)
	Dim systime As SYSTEMTIME    ' the current time
	Dim timestr As String * 260  ' receives the formatted string
	Dim slength As Long          ' length of formatted string returned

	' Retrieve the current time, according to the computer's time zone.
	GetLocalTime systime
	' Format a string to represent the time.
	slength = GetTimeFormat(0, 0, systime, CLng(0), timestr, Len(timestr))
	' Display the string in Text1, found on window Form1.
	Form1.Text1.Text = Left(timestr, slength)
End Function

' *** Place the following code inside Form1. ***

' Create the timer when the form opens and destroy it when the form closes.
' The timer is given an ID of 1, so the return values don't need to be saved.
Private Sub Form_Load()
	Dim retval As Long  ' return value

	retval = SetTimer(Form1.hWnd, 1, 500, AddressOf TimerProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
	Dim retval As Long  ' return value

	retval = KillTimer(Form1.hWnd, 1)
End Sub

See Also

KillTimer

Category

Timers

Back to the Function list.
Back to the Reference section.


Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/settimer.html