CListView中OnTimer()函数只进入一次的问题

今天写一个服务自启程序,遇到了一个问题:在CListView中的OnTimer()函数,只进入一次就不进入了,经过百度查找到了原因:原来在CListCtrl中,基类的的OnTimer()会调用KillTimer,杀死你的定时器

1、下面是原因:

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.解决方法:

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);*/
}


猜你喜欢

转载自blog.csdn.net/u012372584/article/details/79736897