我的C++應用程序有一個無窗口計時器來定期清理永遠不會(也永遠不會)完全處理的潛在通信數據。問題是回調函數從未被調用過。我的類的構造函數執行下面的代碼,就在它返回:SetTimer回調從未被稱爲
if ((this->m_hMsgsThread = ::CreateThread(
NULL, // no security attributes
0, // use default initial stack size
reinterpret_cast<LPTHREAD_START_ROUTINE>(MessagesThreadFn), // function to execute in new thread
this, // thread parameters
0, // use default creation settings
NULL // thread ID is not needed
)) == NULL)
{
dwError = ::GetLastError();
TRACE(_T("%s : Failed to create thread.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
continue;
}
if ((s_pCleanupTimerId = ::SetTimer(NULL, 0, MSGS_CLEANUP_PERIOD, CleanupTimerProc)) == NULL)
{
dwError = ::GetLastError();
TRACE(_T("%s : Failed to create timer.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
continue;
}
這是我CleanupTimerProc
定義:
static void CALLBACK CleanupTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
CMsgDesc * pobjMsgDesc = NULL;
DWORD dwError = ERROR_SUCCESS;
DWORD dwItem;
DWORD dwList;
DWORD dwListItems;
DWORD dwThen, dwNow;
const DWORD cMAX_LISTS = MSGS_MAX_EVENTS;
do
{
// Kill off the old timer.
TRACE(_T("%s : Killing cleanup timer.\r\n"), _T(__FUNCTION__));
ASSERT(s_pCleanupTimerId == idEvent);
::KillTimer(hwnd, idEvent);
// Start a new timer using the same ID.
TRACE(_T("%s : Restarting cleanup timer.\r\n"), _T(__FUNCTION__));
if ((s_pCleanupTimerId = ::SetTimer(NULL, 0, MSGS_CLEANUP_PERIOD, CleanupTimerProc)) == NULL)
{
dwError = ::GetLastError();
TRACE(_T("%s : Failed to create timer.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
continue;
}
// Get the current time.
dwNow = ::GetTickCount();
// Search through the message descriptor lists
// looking for descriptors that haven't been touched in a while.
TRACE(_T("%s : Deleting old message descriptors.\r\n"), _T(__FUNCTION__));
ASSERT(s_pInterface != NULL);
ASSERT(s_pInterface->pobjMessages != NULL);
::EnterCriticalSection(&s_pInterface->pobjMessages->m_csMsgDescriptors);
for (dwList = 0; dwList < cMAX_LISTS; dwList++)
{
dwListItems = s_pInterface->pobjMessages->m_pobjMsgDescriptors[dwList]->GetItemCount();
for (dwItem = 0; dwItem < dwListItems; dwItem++)
{
if ((pobjMsgDesc = (CMsgDesc *)s_pInterface->pobjMessages->m_pobjMsgDescriptors[dwList]->Peek(NULL, dwItem)) == NULL)
{
dwError = ::GetLastError();
TRACE(_T("%s : Failed to peek item from list.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
continue;
}
// Get the last touched time from the descriptor.
dwThen = pobjMsgDesc->m_dwLastTouched;
// If the specified time has elapsed, delete the descriptor.
if ((dwNow - dwThen) > MSGS_DESC_SHELFLIFE)
{
if (s_pInterface->pobjMessages->m_pobjMsgDescriptors[dwList]->Remove(NULL, dwItem) == NULL)
{
dwError = ::GetLastError();
TRACE(_T("%s : Failed to remove item from list.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
continue;
}
delete pobjMsgDesc;
TRACE(_T("%s : Deleted old message descriptor.\r\n"), _T(__FUNCTION__));
}
}
}
::LeaveCriticalSection(&s_pInterface->pobjMessages->m_csMsgDescriptors);
}
while (0);
}
任何想法,爲什麼這功能是沒有得到叫什麼名字?我是否需要在線程內創建計時器?
我希望這不是太不相關的,我不只是錯過了一些東西,但是你怎麼需要摧毀並重新創建計時器? – 2011-08-30 21:32:34
'reinterpret_cast(MessagesThreadFn)'看起來很可疑。我無法想象這可能是正確的。 –