Click to See Complete Forum and Search --> : How To Call Function At Regular Intervals?


Hummer2097
January 13th, 2006, 11:22 PM
How would you go about running a function at set intervals (every 100ms in this case) in a Windows program? Since it’s all event driven I can’t see how you could go about doing it. I tried putting the code in the main window procedure but when the window is inactive it’s not receiving messages so the code doesn’t run on time.

Thanks for any help you can offer.

golanshahar
January 14th, 2006, 05:25 AM
You can do it easily by setting a timer and in the timer callback you call the function you want. look at these functions:


::SetTimer(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timerfunctions/settimer.asp)
::KillTimer(..) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/timerreference/timerfunctions/killtimer.asp)


Now if you need the call at each 100ms will be accurate you should multimedia timers: Multimedia Timer (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_multimedia_timer_functions.asp)

Cheers

Andreas Masur
January 14th, 2006, 06:36 AM
For additional information you might take a look at this article (http://www.codeproject.com/system/timers_intro.asp). It is a very comprehensive timer tutorial.

Hummer2097
January 14th, 2006, 08:54 AM
Thanks a lot for the replies. They're extremely useful and I should be able to get it done now.

Thanks again.