[ACCEPTED]-Execute an operation every x seconds for y minutes in c#-timer

Accepted answer
Score: 15

Just capture the time that you want to stop 5 and end your timer from within the elapsed 4 handler. Here's an example (note: I used 3 a System.Threading.Timer timer. Select the appropriate timer for what 2 you are doing. For example, you might be 1 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...
    }
}
Score: 10

Have your timer loop something like this:

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

while(endTime < DateTime.Now)
{
  // Process loop
}

0

Score: 6

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

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

Score: 3

Timer.Stop() after 120 Ticks.

0

Score: 3

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

Score: 3

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

Score: 2

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

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

Score: 2

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

Score: 2

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;
}

0

More Related questions