jeromekjeromepune
jeromekjeromepune

Reputation:

How to use TIMERS in vc++? I want to run different functions or code after a definite time interval(say 10 ms) without using Sleep()

Hi all i want to execute code similar to a Interrupt service routine after a timer expires(say print hello after every 10ms) and this is a supplementary activity so it has to be using a timer .So how do i implement timers in vc++

Upvotes: 0

Views: 9319

Answers (2)

Naveen
Naveen

Reputation: 73493

Use SetTimer to setup a timer with specified time out. You can either specify a callback method in SetTimer or handle WM_TIMER message. Once done use KillTimer to kill the timer. See this example in MSDN.

Upvotes: 0

i_am_jorf
i_am_jorf

Reputation: 54640

You want to use the CreateTimerQueueTimer() function. Here is the MSDN sample code.

If you need something that runs on older platforms you're stuck with SetTimer(), which delivers a WM_TIMER message to your WNDPROC. It's a pretty terrible API, so I'd really recommend using the Timer Queue function.

CreateTimerQueueTimer() will spawn a worker thread which does the work of figuring out how much time has elapsed and then call you back when it pops. There are threading implications, but it is much nicer overall, especially if you don't want to have a Window.

Upvotes: 2

Related Questions