KillTimer Function

Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

Platforms

Description & Usage

KillTimer deactivates and destroys the specified timer. However, it does not remove any existing WM_TIMER messages which may still be sitting in a window's message queue, if the timer was configured to do so. Your program should destroy any timer created by SetTimer once it is no longer needed.

Return Value

If successful, the function returns a non-zero value. If an error occured, the function returns 0 (use GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

hWnd
A handle to the window that owns the timer. This must be the same value originally passed to SetTimer.
uIDEvent
If the timer was owned by a window, this is the program-defined identifer of the timer. This index was specified in the call to SetTimer. If the timer is not owned by any window, this is the value returned by the call to SetTimer.

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 Form1_Load()
	Dim retval As Long  ' return value

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

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

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

See Also

SetTimer

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/k/killtimer.html