Mr Wednesday
Mr Wednesday

Reputation: 552

Timer is firing before the elapsed Interval

I'm trying to create a Windows Form application that searches for a string and has three possible scenarios:

  1. String 1 found - wait
  2. String 2 found - stop
  3. Else - Perform action and wait 1 minute

I am encountering my problem only on the times when it is expected to wait. When this happens, the newTimer_Tick starts to tick every second. I have tried disabling the timer when it ticks and a few other things but none appeared to work. Below is the code:

public void Action(string result)
{
    if (result.Contains("string1"))
    {
        // Check again in 10 + x seconds
        int n = new Random().Next(0, 5000);
        int newtime = 10000 + n;
        newTimer.Tick += new EventHandler(newTimer_Tick);
        newTimer.Interval = newtime;
        newTimer.Enabled = true;
    }
    else if (result.Contains("string2"))
    {
        // Turn off
        newTimer.Enabled = false;
    }
    else
    {
        // Perform action and tick again in 1min + x seconds
        action1();

        int n = new Random().Next(0, 5000);
        int newtime = 600000 + n;
        newTimer.Tick += new EventHandler(newTimer_Tick);
        newTimer.Interval = newtime;
        newTimer.Enabled = true;
    }
}

private void newTimer_Tick(object sender, EventArgs e)
{
    Action( result );
}

What have I done wrong?

Upvotes: 0

Views: 1700

Answers (3)

André Beltrame
André Beltrame

Reputation: 1

I think what you were missing is that you have to stop your timer since you don't actually want it to keep for more than one interval. You seem to want to run it once, check on the result and then decide if you want to keep running it or not. Here's the code:

  public void action(string result)
    {
        int n = new Random().Next(0, 5000);
        Boolean blActivateTimer = true;
        Timer timer = new Timer();
        timer.Tick += timer_Tick;
        if (!result.Contains("string1") && !result.Contains("string2"))
        {
            n += 600000;
            action1();
        }
        else
        {
            if (result.Contains("string1"))
            {
                n += 10000;
            }
            else
            {
                blActivateTimer = false;
            }
        }
        if (blActivateTimer)
        {
            timer.Start();
        }
    }
    void action1()
    {

    }
    void timer_Tick(object sender, EventArgs e)
    {
        Timer t = (Timer)sender;
        t.Stop();
        action(result);
    }

Upvotes: 0

Phil
Phil

Reputation: 43011

Each time the following line is called, an new instance of the event handler newTimerTick is added to the invocation list for the Tick event:

newTimer.Tick += new System.EventHandler(newTimer_Tick);

So every time the time tick goes off newTimerTick is going to be called multiple times, which is going to give you unexpected results.

Configure your event handler once only. In the constructor would be a sensible place.

Upvotes: 5

Robar
Robar

Reputation: 1971

Have you tried to stop the timer with the Timer.Stop method?

Btw: I don't think you need to reassign the Tick event from the newTimer unless you don't create a new Timer everytime.

Upvotes: 0

Related Questions