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
Who is Participating?
mrmickConnect With a Mentor Commented:
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.

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.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

BabyFaceAuthor Commented:

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

BabyFaceAuthor Commented:
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.?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.