C ++ class - attribute increasing and decreasing every N milliseconds

This should be a simple question, but I cannot find the correct answer to it.

I am coding VS-C ++. I have a custom class 'Person' with a 'height' attribute. I want to call the method of the Grow () class, which starts a timer that will increase the height attribute to 0.5 seconds.

I will have StopGrow () that stops the timer and Shrink () that decreases instead of increasing.

I really need to push a bit on which timer to use and how to use it in the Grow () method. Other methods should be straightforward in knowing this.

That's my first question here, please be kind (and warn me if I'm doing this wrong :) Excuse my English, not my first language.

+1


a source to share


5 answers


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

Don't expose the height element, but use a method such as GetHeight (), which will calculate the height when you need it.



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

No timers needed!

+5


a source


Since you are on Windows, the simplest solution is probably using the GetTickCount () function provided by Windows.

There is no reliable timer function in C ++ that is guaranteed to be less than a second.



So include the header windows.h

and then call GetTickCount()

to get a few milliseconds. The next time you call it, you just subtract the two values, and if the result is greater than 500, half a second has passed.

Alternatively, if you want to block the thread for half a second, use the function Sleep(n)

, where n

is the number of milliseconds you want the thread to sleep. (500 in your case)

+1


a source


You might want to take a look at CreateTimerQueue()

and CreateTimerQueueTimer()

. I've never used them personally, but they would probably fit the bill.

I am currently creating a thread that is responsible for executing timer based operations. It calls WaitForSingleObject()

on the manual-reset event with a timeout of 10ms. It stores an internal collection of callbacks as a pointer to the method and objects for which the callbacks are called. It all hides behind a singlean that provides a scheduler interface that allows the caller's scheduling method to call objects either after the timer expires or regularly at intervals. It looks like the two functions I mentioned should give you pretty much the same functionality ... hmmm ... maybe it's time to revisit this scheduler code ...; -)

+1


a source


Sleep () when a normal timer event is triggered at a 10ms clock rate.
For high-res timer events in windows, use high-res timers

0


a source


Not an easy question! You have at least two options:

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

To make the right decision, you have to think about your application. Does it compute anything (or maybe threads)? Does it interact with the user (maybe the event is driven)? Each approach has some bugs:

  • When you use threads, you have to deal with blocking, which can be tricky.
  • When you do event driven events you have to write asynchronous handlers, which can be tricky.
0


a source







All Articles