views:

174

answers:

2

I create a Windows timer using the following

FHandle := SetTimer(0, 0, 1000, TFNTimerProc(@TMyClass.MyMethod));

Is this thread shown in the Delphi "Threads" window. If Yes how I can get this Thread ID?

+3  A: 

SetTimerdoes not create a thread but does call the specified function in the context of the main thread after the specified timeout. If you don't pass a callback function, SetTimer posts a WM_TIMER message to your main window class.

See the MSDN reference for SetTimer for more information.

Smasher
+5  A: 

There is no thread created by that function. The OS calls the callback function when your program handles the wm_Timer message. It's called from within the context of the same thread that called SetTimer, so the thread had better have a message pump. (It you're calling this from the main VCL thread, then the message pump is provided for you by the TApplication class.)

Furthermore, SetTimer doesn't return a handle. It returns a timer ID.

And finally, unless that method is a class static method, it won't work the way you hope. If the signature of the callback matches what SetTimer expects you to provide, you won't need a type cast, so if you needed to type-cast the function pointer to make the compiler accept your code, you probably got it wrong.

Rob Kennedy
+1 because you spent a minute more than I did and wrote a very complete answer :)
Smasher
Yep, good answer apart from the nitpick about whether an "ID" is a "handle". Some handles *are* just IDs, some handles are pointers. Making the distinction is likely to raise unnecessary questions about how IDs should be handled [sic] differently from handles.
Deltics
Not all handles are IDs, and not all IDs are handles. Conflating the two hinders communication. If you tell people you have a "timer handle," they're liable to think you're talking about a waitable timer, which isn't the same thing at all. Thinking of a timer event ID as a handle might tempt you to call handle-related functions on it, like DuplicateHandle or CloseHandle, but that's not allowed. If the documentation calls something an ID, then call it an ID.
Rob Kennedy