Another SetTimer question
Why is it that the following settimer is not working? the call back
function TimerProc_Wrapper is never called after 5 secs?Why? Thanks
for any help rendered.
I want to be able to interrupt the timedelay instead of using the
sleep(5000).
-------------------------------------------------------------------------------------------------------
Inside the testDoc.h
class CTestDoc : public CDocument
{
//UINT MyThreadFunc(LPVOID lParam);
static UINT_PTR m_TestTimer;
static VOID CALLBACK CTestDoc::TestTimerProc( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD
dwTime );
CWinThread* m_WinThread;
UINT_PTR pTimer;
static void * pObject;
static VOID CALLBACK TimerProc_Wrapper( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD dwTime );
VOID CALLBACK TimerProc( HWND hwnd,
UINT uMsg, UINT idEvent, DWORD dwTime );
void WakeUp();
private:
CRITICAL_SECTION lock;
}
--------------------------------------------------------------------------------------------------------------------
Inside the testDoc.cpp
VOID CALLBACK CTestDoc::TimerProc_Wrapper( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD
dwTime ) {
CTestDoc *pSomeClass = (CTestDoc*)pObject; // cast the void pointer
pSomeClass->TimerProc(hwnd, uMsg, idEvent, dwTime); // call non-
static function
}
VOID CALLBACK CTestDoc::TimerProc(HWND hwnd,
UINT uMsg, UINT idEvent, DWORD dwTime) {
::EnterCriticalSection(&lock);
if(idEvent == pTimer) {
KillTimer(NULL, pTimer); // kill the timer so it won't fire again
MessageBox(NULL,"Timer Killed ",NULL,MB_OK);
m_WinThread->ResumeThread(); // resume the main thread function
}
::LeaveCriticalSection(&lock);
}
UINT WorkerThreadProc( LPVOID Param ) //Sample function for using in
AfxBeginThread
{
CTestDoc* lDoc = (CTestDoc*)Param;
lDoc->pObject = lDoc;
MessageBox(NULL,"Timer Started",NULL,MB_OK);
lDoc->pTimer = SetTimer(NULL, NULL, 5000, lDoc->TimerProc_Wrapper);
lDoc->m_WinThread->SuspendThread();
MessageBox(NULL,"Thread Suspended",NULL,MB_OK);
return TRUE;
}
void CTestDoc::OnTestTest1()
{
// TODO: Add your command handler code here
m_WinThread =
AfxBeginThread(WorkerThreadProc,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
MessageBox(NULL,"Thread Started",NULL,MB_OK);
}
void CTestDoc::WakeUp() {
::EnterCriticalSection(&lock);
KillTimer(NULL, pTimer);
MessageBox(NULL,"Timer Killed(wakeup) ",NULL,MB_OK);
m_WinThread->ResumeThread(); // wake the thread up
::LeaveCriticalSection(&lock);
}