Thread and Timer

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,
I'd like to use a timer inside a thread (CWinThread derived class).
If I use the CALLBACK procedure I get the timer, if I don't use the
CALLBACK and handle in OnTimer I never get the message.
Here is the code:

// WatchDog.cpp : implementation file
//

#include "stdafx.h"
#include "xcrashreport.h"
#include "WatchDog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CString g_sFileName;
HWND g_hWnd;
UINT g_iMessage;
UINT g_iTimer;
UINT g_iTimerTimeout;

/////////////////////////////////////////////////////////////////////////////
// CWatchDog

void CALLBACK OnTimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
)
{ // OnTimerProc

if (idEvent == g_iTimer)
{
PostMessage(g_hWnd, g_iMessage, WPARAM (0), LPARAM (0));


KillTimer( NULL, g_iTimer );
AfxEndThread( 0, false );

}

} // OnTimerProc

IMPLEMENT_DYNCREATE(CWatchDog, CWinThread)

BOOL CWatchDog::InitInstance()
{
// TODO: perform and per-thread initialization here
return TRUE;
}

int CWatchDog::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}

BOOL CWatchDog::IsIdleMessage( MSG* pMsg )
{
if ( !CWinThread::IsIdleMessage( pMsg ) || ( pMsg->message ==
WM_TIMER ) )
{
return FALSE;
}
return TRUE;
}



BEGIN_MESSAGE_MAP(CWatchDog, CWinThread)
//{{AFX_MSG_MAP(CWatchDog)
// NOTE - the ClassWizard will add and remove mapping macros here.

//}}AFX_MSG_MAP

ON_THREAD_MESSAGE(WM_TIMER, OnTimer)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWatchDog message handlers

void CWatchDog::Monitor(CString sFileName, HWND hWnd, UINT iMessage,
int iFrequency, int iTimeout)
{
//g_iTimer = ::SetTimer(NULL, NULL, iFrequency, (TIMERPROC)
OnTimerProc);

g_iTimer = SetTimer(NULL, 0, iFrequency, NULL);
g_hWnd = hWnd;
g_iMessage = iMessage;
}


void CWatchDog::OnTimer(WPARAM wParam, LPARAM lParam)

{
if ((UINT) wParam == g_iTimer)
{
PostMessage(g_hWnd, g_iMessage, WPARAM (0), LPARAM (0));
}
}

Thanks in advance,
Stefano
.



Relevant Pages

  • Re: Thread and Timer
    ... which you blocked the message pump. ... callback timers are about the most useless entities in all of Windows. ... I'd like to use a timer inside a thread. ... UINT g_iMessage; ...
    (microsoft.public.vc.mfc)
  • Re: Variable of Timer ?
    ... UINT SetTimer(UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* ... The timer identifier of the new timer if the function is successful. ... to the KillTimer function to destroy the timer. ...
    (microsoft.public.vc.mfc)
  • Re: EndInvoke still required?
    ... and there will never be another receive callback to ... You could put a timer on the Form, or run a dedicated consumer thread. ... And creating a consumer thread for this one purpose seems wasteful as ... A single extra consumer thread isn't really that big of a deal, and it's not like queuing something on the thread pool is completely cost-free wouldn't otherwise have to worry about). ...
    (microsoft.public.dotnet.framework)
  • Re: [PATCH 2/4] Add cpufreq driver for the IBM PowerPC 750GX
    ... to delete the timer, and then manually set the frequency. ... The first is in the switch callback, ... means the call to _modify_PLLin _target worked. ...
    (Linux-Kernel)
  • Re: About GUIDE Data Types
    ... Could not I create application data through one GUIDE user control and call it from different one? ... If I understand what you're asking, you can access the application data stored in a figure as long as you have its handle, regardless of where (script, regular function, graphics object callback, timer callback, etc.) you're trying to do so. ... handle structure could not be reached by timer and its stop function. ... How did you set the timer function's TimerFcn to use the first function whose signature you provided? ...
    (comp.soft-sys.matlab)