How to reset a timer in C#?
C#TimerC# Problem Overview
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?
C# Solutions
Solution 1 - C#
I always do ...
myTimer.Stop();
myTimer.Start();
... is that a hack? :)
Per comment, on Threading.Timer, it's the Change method ...
> dueTime Type: System.Int32
The
> amount of time to delay before the
> invoking the callback method specified
> when the Timer was constructed, in
> milliseconds. Specify
> Timeout.Infinite
to prevent the
> timer from restarting. Specify zero
> (0) to restart the timer immediately.
Solution 2 - C#
All the timers have the equivalent of Start() and Stop() methods, except System.Threading.Timer.
So an extension method such as...
public static void Reset(this Timer timer)
{
timer.Stop();
timer.Start();
}
...is one way to go about it.
Solution 3 - C#
For System.Timers.Timer
, according to MSDN documentation, http://msdn.microsoft.com/en-us/library/system.timers.timer.enabled.aspx:
> If the interval is set after the Timer has started, the count is > reset. For example, if you set the interval to 5 seconds and then set > the Enabled property to true, the count starts at the time Enabled is > set. If you reset the interval to 10 seconds when count is 3 seconds, > the Elapsed event is raised for the first time 13 seconds after > Enabled was set to true.
So,
const double TIMEOUT = 5000; // milliseconds
aTimer = new System.Timers.Timer(TIMEOUT);
aTimer.Start(); // timer start running
:
:
aTimer.Interval = TIMEOUT; // restart the timer
Solution 4 - C#
You could write an extension method called Reset(), which
- calls Stop()-Start() for Timers.Timer and Forms.Timer
- calls Change for Threading.Timer
Solution 5 - C#
I just assigned a new value to the timer:
mytimer.Change(10000, 0); // reset to 10 seconds
It works fine for me.
at the top of the code define the timer: System.Threading.Timer myTimer;
if (!active)
myTimer = new Timer(new TimerCallback(TimerProc));
myTimer.Change(10000, 0);
active = true;
private void TimerProc(object state)
{
// The state object is the Timer object.
var t = (Timer)state;
t.Dispose();
Console.WriteLine("The timer callback executes.");
active = false;
// Action to do when timer is back to zero
}
Solution 6 - C#
For a Timer (System.Windows.Forms.Timer).
The .Stop, then .Start methods worked as a reset.
Solution 7 - C#
You can do timer.Interval = timer.Interval
Solution 8 - C#
Other alternative way to reset the windows.timer is using the counter, as follows:
int timerCtr = 0;
Timer mTimer;
private void ResetTimer() => timerCtr = 0;
private void mTimer_Tick()
{
timerCtr++;
// Perform task
}
So if you intend to repeat every 1 second, you can set the timer interval at 100ms, and test the counter to 10 cycles.
This is suitable if the timer should wait for some processes those may be ended at the different time span.
Solution 9 - C#
i do this
//Restart the timer
queueTimer.Enabled = true;