Using timers in vc++ via SetTimer and OnTimer

hi . i am using SetTimer(...) combined with OnTimer(...) in my code to call 2 different methods on 2 different timers. Here's a simplified version of my code to make things clear:

SetTimer(1, 100, NULL);

SetTimer(2, 40, NULL);

...

void CMTDlg::OnTimer(UINT nTimerID)

{

if(nTimerID == 1)

{

//capture an image from the webcam and save it in the 'img' folder

}

if(nTimerID == 2)

{

//some processing

}

}

Now, my assumed working of the code was this: After 100 ms timer1 gets fired, OnTimer is called, and an image is saved in the img folder. This repeats every 100 ms. Meanwhile, after every 40 ms, timer2 gets fired, OnTimer is called, and some processing takes place. The capturing of images from the webcam is independent of the the processing in timer2, so regardless of how long that processing takes, my images will be generated every 100 ms by timer1. ie, timer1 and timer2 and mutually independent.

But thats not how its working. The processing in timer2 takes a long time, and this is affecting timer1. timer1 does not generate images every 100 ms now. But if I comment out timer2, then the timer1 generates images fine @ every 100 ms.

So my question is, what am I doing wrong and how can I fix it

Thanks,



Answer this question

Using timers in vc++ via SetTimer and OnTimer

  • dklayman

    You need a second thread I assume.

    The problem is that timers are generated by the message loop. The WM_TIMER messages are only created when the next message is fetched from the message queue. If porcessing stops or takes a long time than timer messages get lost.

    You need a threads that are independant of each other.



  • Scott5

    I think you can leave your (single) timer routine, but take out all the actual processing code. The timer routine should not do any active processing. It should handle the timer message and get the heck out of the way (so other messages can be processed, and, more importantly, so you can get the next timer message and not miss it!!).

    When OnTimer sees the timer event for timer 1, it should spawn a thread to do that processing (capture an image ). And then exit immediately. Similarly when it sees timer event 2, it should spawn a different thread to do your other processing. And then exit immediately.

    To spawn a thread, you can use something like:

    m_winThread = AfxBeginThread( file_thread, &pr, THREAD_PRIORITY_NORMAL );

    where m_winThread is a CWinThread object (in case you need to whack the thread when it gets out of control, or to get its return status) - save this as a member variable so you can get at it later.
    file_thread is the name (only) of you thread processing routine. It should have the prototype

    UINT file_thread( LPVOID pParam );

    &pr is the (address of) parameters you may want to pass to your thread routine. Your thread routine will see it as pParam. You can then cast it to whatever structure or object you like:

    MyParamStruct* pMyParam = (MyParamStruct*)pParam;


    Hope this helps!


  • asamper

    vishal1857 wrote:
    That makes sense. I thought that if I have 2 timers, I will automatically have 2 threads, and these 2 threads will run independent of each other. I assume I was wrong and both the timers run on a single thread only, and hence they run one after the other and not in parallel. Please corrent me on this if I am wrong. Also, if you could suggest some quick code to create 2 threads for the 2 timers, that would be great. Thanks!

    Everything that is involved with windows messages happens in the thread that creates the window. Even a timer is executed always in the same thread context! And timers are depenedant of a message loop.



  • glenna

    That makes sense. I thought that if I have 2 timers, I will automatically have 2 threads, and these 2 threads will run independent of each other. I assume I was wrong and both the timers run on a single thread only, and hence they run one after the other and not in parallel. Please corrent me on this if I am wrong. Also, if you could suggest some quick code to create 2 threads for the 2 timers, that would be great. Thanks!
  • Using timers in vc++ via SetTimer and OnTimer