Question about Timer component

Started by Imortis, January 27, 2021, 03:29:45 PM

Previous topic - Next topic

Imortis

Is there an example of how to use the timer Elapsed event?  I did not see anything, and I may be just be missing something simple.

Paul Squires

No problem, I have attached an extremely simple example that allows you start and stop 2 different timers. You should compile using Win32 Console or Win64 Console because the debug messages are output to the console.
Paul Squires
PlanetSquires Software

Imortis

Awesome.  Thanks for the quick response.

Imortis

Next question.  Is there a way to reset the timer?  Just start it back a zero?  Do you need to disable and enable it to get this effect?

Paul Squires

You can start and stop the timer by manipulating the Enabled property. True will start the timer, whereas False will stop the timer. Behind the scenes the code is calling the Win32 API's SetTimer and KillTimer.

So, to reset the timer you simply need to set the Enabled property to False which effectively kills the existing timer.

Simply changing the Interval value will not reset the timer, although in hindsight, doing so on an active timer should probably effectively kill the current timer and then create a new timer reflecting the new Interval. Currently, I believe you need to Enabled=False, Interval=<value>, Enabled=True.
Paul Squires
PlanetSquires Software

Paul Squires

Quote from: Paul Squires on January 28, 2021, 12:40:30 PM
Simply changing the Interval value will not reset the timer, although in hindsight, doing so on an active timer should probably effectively kill the current timer and then create a new timer reflecting the new Interval. Currently, I believe you need to Enabled=False, Interval=<value>, Enabled=True.

Timer has been modified so that if the Interval property value is changed then the Timer will automatically get re-created with the new interval value thereby saving the programmer from having to manually go through the Enabled=False, Interval-<value>, Enabled=True sequence.
Paul Squires
PlanetSquires Software