Using timers
-
Hi, I am a bit confused on how to make and use a timer. Currently I have
void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); }
wherevoid CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks! -
Hi, I am a bit confused on how to make and use a timer. Currently I have
void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); }
wherevoid CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!OnTimer() is not supposed to be called directly, rather when the timer expires, your app will receive a message and this handler will get called. If you have more than one timer, the nIDEvent should be used to identify it. This is the same value that you passed to SetTimer() as the first argument. So you should specify different values for the first param for each timer attached to a particular window.
-
Hi, I am a bit confused on how to make and use a timer. Currently I have
void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); }
wherevoid CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!I'm not sure if I understood correctly your question. Anyway, you don't have to call OnTimer yourself. This function will be called by the framework when a specific timer fires. The framework will pass in the nIDEvent parameter the Id of the timer that fired. So, for all the timers that expire, you will have only one OnTimer function. In the function, it is up to you to do some specific things depending of the timer (you could use a switch statement and do several different things depending on the Id you receive). Hope that helps.
Cédric Moonen Software developer
Charting control [v1.2] -
OnTimer() is not supposed to be called directly, rather when the timer expires, your app will receive a message and this handler will get called. If you have more than one timer, the nIDEvent should be used to identify it. This is the same value that you passed to SetTimer() as the first argument. So you should specify different values for the first param for each timer attached to a particular window.
Hey thanks for your reply. So in my class where I have defined the function
void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
you're saying it is invoked when the time expires as defined in the SetTimer() function (in my case 2000ms). I put a break point in the function above but unfortunately it was not hit... errr isn't it supposed to be if the function is invoked after the 2000ms? Thanks. -
I'm not sure if I understood correctly your question. Anyway, you don't have to call OnTimer yourself. This function will be called by the framework when a specific timer fires. The framework will pass in the nIDEvent parameter the Id of the timer that fired. So, for all the timers that expire, you will have only one OnTimer function. In the function, it is up to you to do some specific things depending of the timer (you could use a switch statement and do several different things depending on the Id you receive). Hope that helps.
Cédric Moonen Software developer
Charting control [v1.2] -
I'm not sure if I understood correctly your question. Anyway, you don't have to call OnTimer yourself. This function will be called by the framework when a specific timer fires. The framework will pass in the nIDEvent parameter the Id of the timer that fired. So, for all the timers that expire, you will have only one OnTimer function. In the function, it is up to you to do some specific things depending of the timer (you could use a switch statement and do several different things depending on the Id you receive). Hope that helps.
Cédric Moonen Software developer
Charting control [v1.2]Hi, So in my class where I have defined the function
void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
I put a break point in the function above but unfortunately it was not hit... errr isn't it supposed to be if the function is invoked after the 2seconds which was defined in my SetTimer()? Thanks for any advice. -
Hi, I am a bit confused on how to make and use a timer. Currently I have
void MyClass::func() { UINT m_Timer = SetTimer(1, 2000, 0); CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd(); ASSERT(pFrame != NULL); pFrame->OnTimer(1); }
wherevoid CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
I wish the screen to blink every 2 seconds. What I dont understand in the examples I have seen is that OnTimer is not explicitly called. So if you have 2 timers how does each timer know which OnTimer to call? I know that the 1st parameter in the SetTimer function is the nIDEvent which is supposed to be assciociated with the OnTimer() to uniquely identify the timer. I hope that you could provide me with an example where OnTimer is invoked? Thanks!hi, the OnTimer() function is not supposed to be called directly... http://www.codeproject.com/system/timers_intro.asp this will be helpful to you... :)
-
Hey thanks for your reply. So in my class where I have defined the function
void CMainFrame::OnTimer(UINT nIDEvent) { FlashScreen(); CWnd::OnTimer(nIDEvent); }
you're saying it is invoked when the time expires as defined in the SetTimer() function (in my case 2000ms). I put a break point in the function above but unfortunately it was not hit... errr isn't it supposed to be if the function is invoked after the 2000ms? Thanks.yes it should happen after 2s, if its not happening perhaps you have not set up the message map for WM_TIMER?
-
yes it should happen after 2s, if its not happening perhaps you have not set up the message map for WM_TIMER?