For example, say I have the following TimerProc
:
#include <Windows.h>
#define IDT_TIMER1 1001
BOOL g_fKillTimer = FALSE;
VOID DoTimerTask(); // Among other things, sets the flag `g_fKillTimer` when needed
// Timer is started elsewhere with a call to:
// SetTimer(hWnd, IDT_TIMER1, 10000, TimerProc);
VOID CALLBACK TimerProc(
_In_ HWND hWnd,
_In_ UINT uMsg,
_In_ UINT_PTR idEvent,
_In_ DWORD dwTime
)
{
if (TRUE == g_fKillTimer)
{
KillTimer(hWnd, IDT_TIMER1);
g_fKillTimer = FALSE;
}
DoTimerTask();
}
Is this code going to successfully kill the timer IDT_TIMER1
? Or is the fact that I'm calling KillTimer
within the timer procedure itself potentially going to present a problem for this? Will the function DoTimerTask()
still be called?
Is this code going to successfully kill the timer
IDT_TIMER1
?
Yes.
is the fact that I'm calling
KillTimer
within the timer procedure itself potentially going to present a problem for this?
No. It is perfectly safe to call KillTimer
inside the timer callback.
Will the function
DoTimerTask()
still be called?
Yes, since you are not return
'ing from the callback before calling DoTimerTask()
again, eg:
VOID CALLBACK TimerProc(
_In_ HWND hWnd,
_In_ UINT uMsg,
_In_ UINT_PTR idEvent,
_In_ DWORD dwTime
)
{
if (TRUE == g_fKillTimer)
{
KillTimer(hWnd, IDT_TIMER1);
g_fKillTimer = FALSE;
return; // <-- HERE
}
DoTimerTask();
}