There are three Timer
classes that I am aware of, System.Threading.Timer
, System.Timers.Timer
, and System.Windows.Forms.Timer
, but none of these have a .Reset()
function which would reset the current elapsed time to 0.
Is there a BCL class that has this functionality? Is there a non-hack way of doing it? (I thought perhaps changing the time limit on it might reset it) Thought on how hard it would be to reimplement a Timer
class that had this functionality, or how to do it reliably with one of the BCL classes?
i do this
For a Timer (System.Windows.Forms.Timer).
The .Stop, then .Start methods worked as a reset.
I just assigned a new value to the timer:
it works fine for me.
at the top of the code define the timer:
System.Threading.Timer myTimer;
For
System.Timers.Timer
, according to MSDN documentation, http://msdn.microsoft.com/en-us/library/system.timers.timer.enabled.aspx:So,
You could write an extension method called Reset(), which
For clarity since some other comments are incorrect, when using
System.Timers
setting Enabled to true will reset the elapsed time. I just tested the behavior with the below:I would input text to the text box repeatedly and the timer would only run 3 seconds after the last keystroke. It's hinted at in the docs as well, as you'll see: calling
Timers.Start()
simply sets Enabled to true.And to be sure, which I should've just went straight to from the beginning, you'll see in the .NET reference source that if enabling an already Enabled timer it calls the private
UpdateTimer()
method, which internally callsChange()
.