Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1646
  • Last Modified:

How to use CWnd::SetTimer() in a worker thread?

Hi all, first time posting and i am totally new to visual C++.

  I am creating a game and the screen require constant redraw. I implemented the ON_WM_TIMER()function so that it will set off my OnTimer function in my dialog class using SetTimer(...) function. It works fine, just that my game run way too slow.
  I thought of calling the SetTimer in a seperate thread function. Will it help? I can't seems to get it to work. All i did was this:

class CClienttestDlg : public CDialog {


CWinThread*     mThread;
static UINT CClienttestDlg::StartTimer(LPVOID pParam)

BOOL CClienttestDlg::OnInitDialog()

mThread = AfxBeginThread(CClienttestDlg::StartTimer,(LPVOID)myTimerID);

UINT CClienttestDlg::StartTimer(LPVOID pParam) {
          // -- set the timer
     uResult = SetTimer((UINT)pParam, (1000/frame_rate), NULL);
     return 0 ;

the error is that i cannot use a non static function in my thread. is there anyway i can slove my problem? thanks

1 Solution
you can use multimedia timers

The timeSetEvent function starts a specified timer event. The multimedia timer runs in its own thread. After the event is activated, it calls the specified callback function or sets or pulses the specified event object.

MMRESULT timeSetEvent(
 UINT uDelay,                
 UINT uResolution,          
 DWORD dwUser,              
 UINT fuEvent                

or you can create a thread of your own and control a timer there (see link below)

create a structure for passing thread arguments
UINT m_nTimerID;
CClienttestDlg *m_pDlg;

FIll this struct and passs it as arg.

pThreadInfo->m_nTimerID = myTimerID;
pThreadInfo->m_pDlg = this;
mThread = AfxBeginThread(CClienttestDlg::StartTimer,(LPVOID)pThreadInfo);

Then, in therad function

UINT CClienttestDlg::StartTimer(LPVOID pParam) {
         // -- set the timer
THREADINFO *pThreadInfo = static_cast<THREEADINFO*>(pParam);
    uResult = pThreadInfo->m_pDlg->SetTimer((UINT)pThreadInfo->m_nTimerID, (1000/frame_rate), NULL);
delete pThreadInfo;
    return 0 ;


Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now