Click to See Complete Forum and Search --> : Timer to a thread


Tejas
November 18th, 2004, 09:01 AM
Hiello everyone,

can any one help me in this.
I want to know to set a timer to a thread,
Can any one help to set a timer to a thread.

Thanks in advance
NAresh :confused:

JohnCz
November 18th, 2004, 09:21 AM
Use SetTimer passing timer procedure.

ovidiucucu
November 18th, 2004, 09:33 AM
For a GUI thread you can use SetTimer with first parameter (HWND) NULL and handle WM_TIMER message:
class CMyThread : public CWinThread
{
// Attributes
protected:
UINT_PTR m_nTimerID;
// ...
// Generated message map functions
//{{AFX_MSG(CMyThread)
// ...
//}}AFX_MSG
afx_msg void OnTimer(UINT nIDEvent, LPARAM);
DECLARE_MESSAGE_MAP()
};
CMyThread::CMyThread() : m_nTimerID(0)
{
}
BOOL CMyThread::InitInstance()
{
m_nTimerID = ::SetTimer(NULL, 0, 2000, NULL);
return TRUE;
}
int CMyThread::ExitInstance()
{
if(0 != m_nTimerID)
{
::KillTimer(NULL, m_nTimerID);
}
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CMyThread, CWinThread)
//{{AFX_MSG_MAP(CMyThread)
// ...
//}}AFX_MSG_MAP
ON_THREAD_MESSAGE(WM_TIMER, OnTimer)
END_MESSAGE_MAP()
// ...
void CMyThread::OnTimer(UINT nIDEvent, LPARAM)
{
switch(nIDEvent)
{
case 0:
// ...
break;
// ...
}
}

Tejas
November 18th, 2004, 09:40 AM
Hi thanks for the reply,
But my problem is i have a thread created in seperate class using create thread an any one tell me how to associate timer to that thread
:confused:

JohnCz
November 18th, 2004, 09:49 AM
ovidiucucu just answer your question. This is for a class derived from a CWinThread.
Unless
Hi thanks for the reply,
But my problem is i have a thread created in seperate class means that you have workers thread this is valid answer.
If you are using worker thread then my answer is valid.
It is valid for both cases anyway.

Andreas Masur
November 18th, 2004, 09:59 AM
[ Moved thread ]

ovidiucucu
November 18th, 2004, 10:21 AM
:confused:
That happens when a question gives too few details. We must guess what you want.
As John already said, both answers are valid. Next time, please let us know more about your problem from the beginning.