I want to create a 0 to 1 value that I will get from my timer. The value will describe how long it is until the next tick. 0 is the furthest away from the tick (in milliseconds) 0.5 is halfway to the tick and 1 is when it ticks.
How can I achieve this?
One simple option would be to use a Stopwatch
. Start it when you start the timer, and call Restart
at the start of each tick handler - indeed, you can do this separately by adding a handler just to do the stopwatch restart before you add the "real" handler.
Then you'd use:
double progress = stopwatch.Elapsed.TotalMilliseconds / timer.Interval;
Bear in mind that could be more than 1 occasionally, as the timer may fire late depending on what else the UI thread is doing. Note that this provides a double
rather than a decimal
, but I think that's more appropriate for this situation.
How you encapsulate this really depends on how what you're trying to do and how the rest of your program hangs together... we don't have enough information to guide you there at the moment.
See more on this question at Stackoverflow