tags:

views:

2681

answers:

9

Hello,

i need to run a function every 5 seconds for 10 minutes.

I use a timer to run it for 5 secs, but how do I limit the timer for only 10 mins?

Thank you very much

Andrea

A: 

Timer.Stop() after 120 Ticks.

JP Alioto
Negative love for the simple answer? :)
JP Alioto
A: 

Note the start time. In each call, test if currentTime + 5 seconds > startTime + 10 minutes. If so, disable the timer.

I prefer this approach to just running for N ticks, as timers are not guaranteed to fire when you'd like them to. It's possible 120 ticks may run over 10 minutes of real world time.

Adam Wright
+2  A: 

just use a DateTime variable to track when it should end and set that right before you start. The on your Elapsed event handler, check if the signal time is less than the end time. If it isn't, stop the timer.

Darren Kopp
+1  A: 

You can calculate how times your function will be call, and create decrement counter, after elapsed which you unsubscribe from timer tick. Or you can Run another timer which have tick period - 10 min and on tick you unsubscribe from timer tick calling your function.

Maksim Kondratyuk
+4  A: 

Divide the Y minutes by the X interval to get how many times it needs to run. After that you just need to count how many times the function has been called.

In your case, 10 min = 600 seconds / 5 seconds = 120 calls needed. Just have a counter keep track of how many times your function has been called.

+6  A: 

Have your timer loop something like this:

DateTime endTime = DateTime.Now.AddMinutes(10);

while(endTime < DateTime.Now)
{
  // Process loop
}
Peter Mourfield
I would rather store the *end time* and compare that with present time value before each iteration. Nevertheless, +1.
Cerebrus
You're right, storing the end time would be better.
Peter Mourfield
A: 

You can set two timers one that run for 5 secs and the other one that run for 10min and disable the first one

Yassir
+5  A: 

Just capture the time that you want to stop, and end your timer from within the elapsed handler. Here's an example (note: I used a System.Threading.Timer timer. Select the appropriate timer for what you are doing. For example, you might be after a System.Windows.Forms.Timer if you are writing in Winforms)


public class MyClass
{
    System.Threading.Timer Timer;
    System.DateTime StopTime;
    public void Run()
    {
     StopTime = System.DateTime.Now.AddMinutes(10);
     Timer = new System.Threading.Timer(TimerCallback, null, 0, 5000);
    }

    private void TimerCallback(object state)
    {
     if(System.DateTime.Now >= StopTime)
     {
      Timer.Dispose();
      return;
     }
     // do your work.....
    }
}

JMarsch
A: 

You could use a second timer:

class Program
{
    static void Main(string[] args)
    {
        int interval = 5 * 1000; //milliseconds
        int duration = 10 * 60 * 1000; //milliseconds

        intervalTimer = new System.Timers.Timer(interval);
        durationTimer = new System.Timers.Timer(duration);

        intervalTimer.Elapsed += new System.Timers.ElapsedEventHandler(intervalTimer_Elapsed);
        durationTimer.Elapsed += new System.Timers.ElapsedEventHandler(durationTimer_Elapsed);

        intervalTimer.Start();
        durationTimer.Start();
    }

    static void durationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        intervalTimer.Stop();
        durationTimer.Stop();
    }

    static void intervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //call your method
    }

    private static System.Timers.Timer intervalTimer;
    private static System.Timers.Timer durationTimer;
}
scottm