tags:

views:

6124

answers:

6

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?

+10  A: 

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.

JP Alioto
The other issue is that only woeks with Forms.Timer, and my app has no GUI (Application.Start() with no parameters), so I THINK that the Threading.Timer class is better for other reasons, but good point.
Matthew Scharley
@Matthew: See http://msdn.microsoft.com/en-us/magazine/cc164015.aspx for a discussion of the various timer classes and when using them is appropriate. In general, though, `Forms.Timer` should only be used with a GUI. However, besides `Forms.Timer` and `Threading.Timer` there is also `Timers.Timer`.
Brian
+5  A: 

I believe they all 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 this.

Dan
I'm not looking to measure elapsed time. My exact usecase for this is that I have a FileSystemWatcher watching a directory and want to catch groups of changes (ie, changes made within for example 5s of each other). The theory being that the first change starts a timer that gets reset with each change till it eventually fires and closes off the group.
Matthew Scharley
Yeah, I spotted that when I re-read your question. Edited my answer.
Dan
+1  A: 

You could write an extension method called Reset(), which

  • calls Start()-Stop() for Timers.Timer and Forms.Timer
  • calls Change for Threading.Timer
Gishu
A: 

there is no any other timer as you have specified except than window.forms.timer in vs2008

anjali
A: 

other alternative way to reset the windows.timer is using the counter, as follows:

int tmrCtr=0; Timer mTimer;

private void ResetTimer() { tmrCtr=0; }

private void mTimer_Tick() { tmrCtr++; //perform task }

so if you intend to repeat everi 1 second, you can set the timer interval at 100ms, and test the counter to 10 cycles.

this suitable if the timer should wait for some processes those my be ended at the different time span.

ivan
A: 

For a Timer (System.Windows.Forms.Timer).

The .Stop, then .Start methods worked as a reset.

elmsoftware