Monday, March 10, 2008

SetTimer() inside a thread - use SetTimer API method instead and pass a callback

CWinThread does not have a SetTimer method. So you can not use

BEGIN_MESSAGE_MAP(CArc220Thread, CWinThread)
ON_WM_TIMER()
END_MESSAGE_MAP()

or

myThreadPointer->SetTimer(1, 500, NULL);

Use the SetTimer API method instead and pass a callback. Make sure the HWND parameter is NULL.

============ from the thread's .h file ============

// NOTE: this is NOT declared inside the class

void CALLBACK EXPORT processArc220Data(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
);

============ from the thread's .cpp file =============

timerValue = ::SetTimer(NULL, IDT_ARC220_TIMER, 1000, (TIMERPROC) processArc220Data);
.
.
.
void CALLBACK processArc220Data(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
.
.
.
::KillTimer(NULL, timerValue);




No comments: