Re: A _very_ basic question about WM_TIMER and threading
As already noted here, your handler code probably spun a message loop
(through e.g. a modal dialog box, call to a COM component in another
apartment etc).
What you could try is to kill/re-enable a timer inside OnTimer.
Imagine (warning: compiled with head-compiler and tried with head-
debugger ;-) ):
class CTempTimerDisable
{
public:
CTempTimerDisable(CWnd* pWnd, UINT nTimer, UINT nPeriod) :
m_pWnd(pWnd), m_nTimer(nTimer), m_nPeriod(nPeriod)
{
VERIFY(m_pWnd->KillTimer(m_nTimer));
}
~CTempTimerDisable()
{
> VERIFY(m_pWnd->SetTimer(m_nTimer, m_nPeriod) == m_nTimer);
}
// Maybe useful: void DisablePermanently() { /*set a flag to suppress
setting a timer in dtor*/ }
private:
// needed data members here.
// good idea to make this non-copyable
};
Then, in OnTimer
void CMyWnd::OnTimer(UINT nTimer)
{
CTempTimerDisable DisableCurrentTimer(this, nTimer, nTimerPeriod);
// Handler, without fear of nTimer firing again.
}
You may want additional error handling in CTempTimerDisable ctor. You
may need to account for SetTimer failing (I've never seen this
actually happen, but...).
Note that other your other timers still may fire, which may be
undesirable.
HTH,
Goran.
Goran,
Thanks for the reply. I'm afraid that your suggestion might not work for
me. I'd like the timer to keep 'ticking' to avoid unnecessary delays. For
example, let's say that the timer is set to expire every 1000ms and the
timer handler code takes 800ms to execute (the worst case scenario). If I
disable and then re-enable the timer then I'm going to delay the next
handler execution by ~800ms. This would not be acceptable in my case.
Also, another problem is that disabling the timer does not guarantee to
solve the re-entrancy problems. There might still be a chance - I think -
that another WM_TIMER message might be placed in the queue by the time I
finish a call to KillTimer(). I'm not sure about it, but the docs say that
KillTimer() does not remove corresponding WM_TIMER messages already in the
queue.
Thanks.