Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am streaming from a camera and at the same time I am trying to display the seconds on a static box.On a button click event, while(EventMsg.message != WM_QUIT) I am streaming and displaying the images from the camera.Now to display the seconds I am using MFC timers and when WM_TIMER is notified to window on each 1000ms my CStreamDlg::OnTimer(UINT nIDEvent) function is supposed to be called which is not happening.
My CStreamDlg::OnTimer(UINT nIDEvent) function looks like the following:

void CStreamDlg::OnTimer(UINT nIDEvent) {
    char msg[10];
    if(nIDEvent == ID_TIMER_SECOND){
        iSeconds+=1;
        sprintf_s(msg,10,"%d",iSeconds);
        ((CStatic*)GetDlgItem(IDC_STATIC_SECS))->SetWindowTextA(msg);
    }
}

Thanks in advance
Posted
Comments
harish85 29-Jun-11 3:11am    
does it get called once?
Can you put info on your settimer ,I dont see you calling Ontimer again..
Sergey Alexandrovich Kryukov 29-Jun-11 3:27am    
Oh, timers... who needs them? Use threads.
--SA
Joan M 29-Jun-11 3:43am    
Which advantages would have using threads to get a timer behavior? (being curious here)
Sergey Alexandrovich Kryukov 29-Jun-11 4:57am    
Oh, I can explain... timers cause much more trouble. Imagine what happens if a timer callback code is not finished when a new timers event comes. Imagine this timing (execution time of the callback code) is probabilistic (in essence, it's always so). This is OK of course, but the logic can rely on the fact it does not happen. Threads a free from this; in worse case it will be delayed a bit, but you can always check real time inside code (if it's needed for animation of physics, for example).
--SA
Joan M 29-Jun-11 5:06am    
Yes... sounds correct... I guess everything depends on the work loads and on what does one expect to get from the "timer" itself... anyway I had never thought on using threads and not timers... it is an interesting approach... I'll take a deeper look at it whenever I need another timer somewhere... ^^

1 solution

At the end of your OnTimer call you should put something like
CDialog::OnTimer(nIDEvent);


And you should configure and start the timer using
m_nTimer = SetTimer (ID_TIMER_SECOND, 1000, 0);


And don't forget to kill the timer at the end...
Killtimer(m_nTimer);


Hope this helps... :thumbsup:

PS: I'm sure that the proposal from SAKryukov will be at least interesting so it would be nice for you to take a look at it... meanwhile, as he is suggesting to use threads to get the same behavior, and I don't know if you are comfortable using them or not or even if you can do it from your requirements, here you have a classic timer approach...


Two links that explain timers in a nice way:

The first one is from Nemanja Trifunovic[^] and is a timers tutorial[^].

The second one is from a commercial company that will try to sell their product[^]... Anyway, you can get some extra information and numbers on the different type of timers out there...
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Jun-11 4:57am    
Yes, my 5.
--SA
Joan M 29-Jun-11 5:04am    
Thank you SAKryukov!
hakz.code 29-Jun-11 7:35am    
Thanks Joan Murt,I got it working, I missed CDialog::OnTimer(nIDEvent); part of code which is added automatically if we add timer using the class view->properties->Timers->WM_TIMER->Add onTimer()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900