PRB: OnTimer() Is Not Called Repeatedly for a List Control

This article was previously published under Q200054
This article has been archived. It is offered "as is" and will no longer be updated.
If you call the SetTimer function to send periodic WM_TIMER messages to a list control, you may find that the WM_TIMER message handler (the OnTimer function) for a list control is called only twice.
The WM_TIMER handler in the list control calls the KillTimer function.
If you define the timer ID in the SetTimer call, do not call the default handler for WM_TIMER.
This behavior is by design.
The list control uses the timer for editing labels, and for scrolling. When you handle the timer message, if the timer ID is your own timer, don't call the default handler (CListCtrl::OnTimer).

In the sample code below, without the if clause in the CMyListCtrl::OnTimer function, the default WM_TIMER handler is always called, which destroys both the control's timer and your own timer. As a result, you should see a trace statement for each timer.
void CGensdiView::OnInitialUpdate() {   CView::OnInitialUpdate();	   m_pCtrl = new CMyListCtrl;   m_pCtrl->Create(WS_VISIBLE | WS_CHILD | WS_BORDER                    | LVS_REPORT | LVS_EDITLABELS,                    CRect(0, 0, 100, 150), this, 2134);   m_pCtrl->InsertColumn(0, "one");   m_pCtrl->InsertItem(0, "0 0 0 0 0 0 0 0 0 0 0");   m_pCtrl->InsertItem(1, "1 1 1 1 1 1 1 1 1 1 1");   m_pCtrl->InsertItem(2, "2 2 2 2 2 2 2 2 2 2 2");}void CGensdiView::OnLButtonDown(UINT nFlags, CPoint point) {   m_pCtrl->m_timerID = m_pCtrl->SetTimer(14, 500, NULL);   TRACE("timer %d is set\n", m_pCtrl->m_timerID);}CMyListCtrl::CMyListCtrl(){   m_timerID = -1;}void CMyListCtrl::OnTimer(UINT nIDEvent){   if (nIDEvent != m_timerID)      CListCtrl::OnTimer(nIDEvent);   TRACE("OnTimer %d\n", nIDEvent);}				
CListview CListctrl WM_TIMER OnTimer KillTimer SetTimer once

Article ID: 200054 - Last Review: 02/23/2014 01:12:35 - Revision: 2.1

Microsoft Foundation Class Library 4.2

  • kbnosurvey kbarchive kbtimer kblistview kbprb KB200054