We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now



BabyFace asked
Medium Priority
Last Modified: 2012-06-27

When setting a Timer using the API func. SetTimer,
How can you make sure that the handle of the timer you're
providing isn't already in use?
Is there a function that returns the next valid timer handle?
Something like FreeFile???

Thanks in advance
Watch Question

The SetTimer API function creates a timer, the return value is a long integer identifying the new timer.  It is unique.

There is a Timer control with VB - it's easier to use.


If the return value is zero, the function failed.



Perhapse I should make my question a little clearer.

This is the definition of SetTimer:
Declare Function SetTimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

How do I find the next valid timer identifier?
because what if you did this?
SetTimer me.hwnd,1,1000,addressof Myfunc
[in another function]
SetTimer me.hwnd,1,1000,addressof MyFunc2
[destory event]
KillTimer 1
???? What about the other timer?????

As I said, the timer function RETURNS a long integer identifying the new timer. It is unique.  When set the timer like this:

TimerID = SetTimer me.hwnd,1,1000,addressof Myfunc

And release it:

Kill TimerID

The second argument in you're example (1) is the EVENT ID


TimerID = SetTimer me.hwnd,1,1000,addressof Myfunc
And release it:
Kill TimerID

I suppose you ment KillTimer TimerID right?
But the description of the KillTimer say that it receives a windows hwnd  and the timer identifier
not the timer handle.
So how do you find the next valid timer handle.?

Well, if you look at the SetTimer description - you'll find the following:

Return Values
If the function succeeds, the return value is an integer identifying the new timer. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. If the function fails to create a timer, the return value is zero.

The syntax for releasing the timer would therefore be:

KillTimer hWnd, TimerID

What makes the TimerID unique is the hWnd.  You must specify a different ID if you're going to create a second timer using the same hWnd.  In your example, 1000 would be the only timer associated with me.hwnd.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.