1

I am using a timer to run a method every 16 minutes. I also want to run a second method every minute for 15 minutes in-between.

Below is the code I am using:

int count =  0;

private void cmdGo_Click(object sender, EventArgs e)
{
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
        t.Interval = 960000; // specify interval time - 16 mins
        t.Tick += new EventHandler(timer_Tick);
        t.Start();
}

void timer_Tick(object sender, EventArgs e)
{
        RunMethod1();

        while(count < 15)
        {
            //waiting for 60 seconds 
            DateTime wait = DateTime.Now;
            do
            {
                 Application.DoEvents();
            } while (wait.AddSeconds(60) > DateTime.Now);

            RunMethod2();
        }
}

The above code seems to work fine but the ‘do while’ loop to wait for 60 seconds is very CPU heavy.

I tried to use Thread.Sleep(60000) but this freezes up the Interface and also tried to add a second timer within timer_Tick but this doesn’t seem possible. Can a second timer be added within the EventHandler of the first?

Is there any other method to achieve this without being so CPU intensive?

Thanks! Warren

NOTE: Sorry guys, there was a typo in my original post. The 60 second wait do, while loop should have been within the while < 15 loop. Just updated the code snippet.

So:

  • RunMethod1() should be executed every 16 mins
  • RunMethod2() should be executed every 1 min (15 times) in between the 16 min tick

2 Answers 2

1

It would make more sense to have a counter to store how many times the clock has gone off. Then set your timer interval to fire once a minute so not doing anything in between...

That way you could just do...

private int Counter;

private void cmdGo_Click(object sender, EventArgs e)
{
    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    t.Interval = 60000; // specify interval time - 1 minute
    t.Tick += new EventHandler(timer_Tick);
    t.Start();
}

// Every 1 min this timer fires...
void timer_Tick(object sender, EventArgs e)
{
    // If it has been 16 minutes then run RunMethod1
    if (++Counter >= 16)
    {
        Counter = 0;
        RunMethod1();         
        return;
    }

    // Not yet been 16 minutes so just run RunMethod2
    RunMethod2();
}
6
  • Your code and OP's code are semantically different. OP's code calls RunMethod1 after 16 minutes but yours will call it in 1 minute Apr 29, 2014 at 9:17
  • The OP question was how to run Method1 every minute and Method2 every 16 minutes... This is how you can achieve that?
    – Belogix
    Apr 29, 2014 at 9:19
  • If that's what being asked, you're rite. I was confused to see The above code seems to work fine. It is not clear what OP wants :( If this is what he wants his code shouldn't work isn't it? Apr 29, 2014 at 9:22
  • Sorry, there was a typo in my original post :( Have updated it now, please see above Apr 29, 2014 at 10:32
  • @user3584576 - I have changed code slightly. You can see that RunMethod2 will run every minute for 15 minutes and on the 16th minute then RunMethod1 will execute and so on. This is clean, easy to read and doesn't mess about with Thread.Sleep etc.
    – Belogix
    Apr 29, 2014 at 12:44
1

You could await a task Delay so the UI will keep responding

    async void timer_Tick(object sender, EventArgs e)
    {
        RunMethod1();



        while (count < 15)
        {
            //waiting for 60 seconds 
            await Task.Delay(60000);

            RunMethod2();
        }
    }
4
  • 1
    Don't you aware of Task.Delay ? Task.Run(() => Thread.Sleep(60000)) unnecessarily wasting 1 thread for 1 minute. Apr 29, 2014 at 9:13
  • This would run RunMethod2 15 times in quick succession rather than one minute between each one.
    – Belogix
    Apr 29, 2014 at 13:08
  • @Belogix Agreed, only replied to the piece of offending code of the OP's original code. Altered the wait location to the new location. However, I also didn't go into the underlying question. Taking that into account, the OP is better off with an alternative method such as in your post.
    – Me.Name
    Apr 29, 2014 at 13:25
  • This code will run less than once/minute. The time RunMethod1/2 take to execute will be added on every minute, meaning you'll drift, possibly by a significant amount, depending on what you're doing / how long your app has been running.
    – Basic
    Jul 18, 2015 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.