user12381459
user12381459

Reputation:

Why kill a timer from within the timer callback?

In Raymond's example of how to use SetTimer to distinguish single and double-clicks, he calls KillTimer from within the timer callback function. I wasn't clear on why that would be necessary. Doesn't the fact that the callback is occurring mean that the timer has reached its endpoint?

Upvotes: 0

Views: 300

Answers (2)

IInspectable
IInspectable

Reputation: 51413

Oddly enough, the documentation for SetTimer doesn't document its behavior. The behavior being that the call installs an auto-repeat timer.

However, you will find the following under timer operations:

Whenever the time-out value for the timer elapses, the system posts a WM_TIMER message to the window associated with the timer.

This API has no immediate support for one-shot timers. To implement a one-shot timer you have to call KillTimer when handling the first WM_TIMER message.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612993

Timers fire repeatedly at their specified frequency. You must call KillTimer if you wish them to stop firing.

So a classic usage is to create a timer, and then kill it the first time it fires. That's known as a one shot timer.

Upvotes: 0

Related Questions