views:

378

answers:

5

Hello all!

This must be an easy question but I can't find a properly answer to it.

I'm coding on VS-C++. I've a custom class 'Person' with attribute 'height'. I want to call class method Grow() that starts a timer that will increment 'height' attribute every 0.5 seconds.

I'll have a StopGrow() that stops the timer and Shrink() that decrements instead of increment.

I really need a little push on which timer to use and how to use it within Grow() method. Other methods must be straight forward after knowing that.

That's my first question here so please be kind (and warn me if I'm doing it wrong :) Forgive my English, not my first language. Thanks in advanced.

+1  A: 

Since you're on Windows, the simplest solution is probably to use the GetTickCount() function supplied by Windows.

There isn't a good timer function in the C++ language with a precision guaranteed to be less than a second.

So instead, include the windows.h header, and then call GetTickCount() to get a number of milliseconds. The next time you call it, you simlpy subtract the two values, and if the result is over 500, half a second has elapsed.

Alternatively, if you want to block the thread for half a second, use the Sleep(n) function, where n is the number of milliseconds you want the thread to sleep. (500 in your case)

jalf
A: 

Sleep() an the normal timer event run off a 10ms clock.
For high resolution timer events on windows use high resolution timers

Martin Beckett
A: 

Not an easy question at all! You have at least two possibilities:

  • create a thread that will execute a loop: sleep 0.5s, increase height, sleep 0.5s, increase height, etc.
  • invert flow of control and pass it to some framework like Boost::Asio that will call your timer handler in every 0.5s.

In order to make the right decision you have to think about your whole application. Does it compute something (then maybe threads)? Does it interact with the user (then maybe event driven)? Each approach has some gotchas:

  • When you use threads you have to deal with locking, which can be tricky.
  • When you do event-driven stuff, you have to write asynchronous handlers, which can be tricky.
Jasiu
+1  A: 

You might want to take a look at CreateTimerQueue() and CreateTimerQueueTimer(). I've never personally used them, but they would probably fit the bill.

I currently spawn a thread that is responsible for doing timer based operations. It calls WaitForSingleObject() on a manual-reset event with a 10ms timeout. It keeps an internal collection of callbacks in the form of pointer-to-method and objects that the callbacks are invoked for. This is all hidden behind a singleton that provides a scheduler interface that let's the caller schedule method calls on the objects either after a timer expiration or regularly on an interval. It looks like the two functions that I mentioned should give you pretty much the same functionality... hmmm... might be time to revisit that scheduler code... ;-)

D.Shawley
+5  A: 

Do you really need to call the code every half second to recalculate a value? For most scenarios, there is another much simpler, faster, effective way.

Don't expose a height member, but use a method such as GetHeight(), which will calculate the height at the exact moment you need it.

Your Grow() method would set a base height value and start time and nothing else. Then, your GetHeight() method would subtract the starting time from the current time to calculate the height "right now", when you need it.

No timers needed!

Euro Micelli