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

Posted on 2003-04-01
Medium Priority
Last Modified: 2013-11-20
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

Question by:jianwei
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

williamcampbell earned 225 total points
ID: 8248008
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)


Expert Comment

ID: 8251039
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question