The problem that the OnTimer() function in CListView only enters once

Today, I wrote a service self-starting program, and encountered a problem: the OnTimer() function in CListView will not enter after only one entry. After Baidu search, I found the reason: it turns out that in CListCtrl, the OnTimer() of the base class will call KillTimer, kills your timer .

1. Here are the reasons:

SYMPTOMS 
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.

CAUSE 
The WM_TIMER handler in the list control calls the KillTimer function.

RESOLUTION 
If you define the timer ID in the SetTimer call, do not call the default handler for WM_TIMER.

STATUS 
This behavior is by design.

MORE INFORMATION 
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.

2. Solution:

SetTimer(101,10000,NULL);
	SetTimer(100,20000,NULL);
void CJTVServMonitorView::OnTimer(UINT_PTR nIDEvent)
{
	if (100 == nIDEvent)
	{
		InsertServers();
	}
	else if (101 == nIDEvent)
	{
		MonitorServ();
	}
	else
	{
		CListView::OnTimer(nIDEvent);
	}

	/*CListView::OnTimer(nIDEvent);*/
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326312006&siteId=291194637