2

I am trying to implement a timer in my application with three buttons - Start, Stop and Restart.

The timer starts and stops fine. However, when I click on the restart button it starts from the previous value of seconds and minutes instead of starting at 0 and 0. I can't see what I'm doing wrong as I set seconds and minutes to 0 right in the start of the restart method itself.

eg: First time starts at 0 and goes on to 1, 2, 3. Stops at 3. When I restart, it starts from 2 and goes on to 4, 6... If I restart again it starts at 3 and goes on to 6, 9, 12...

Below is my code:

    public int seconds;
    public int minutes;
    public int hours;

    System.Windows.Threading.DispatcherTimer myTimer = new System.Windows.Threading.DispatcherTimer();

    private void Tick_Timer_Start(object sender, RoutedEventArgs e)
    {
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();
    }

    private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
    {
        myTimer.Stop();
    }

    private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
    {
        seconds = 0;
        minutes = 0;
        Timertext.Text = "0 minutes : 0 seconds";
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();

    }

    private void Each_Tick(object o, EventArgs sender)
    {
        seconds = seconds + 1;

        if (seconds < 10 && minutes < 10)
        {
            Timertext.Text =  minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
        }
        else if (seconds > 59)
        {
            minutes = minutes + 1;
            seconds = 0;
            seconds = seconds + 1;
            Timertext.Text =  minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
        }
        else
        {
            Timertext.Text =  minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
        }
    }

Any help will be much appreciated.

1
  • 1
    The problem is that you add a new Tick handler on each restart. Just remove the line myTimer.Tick += new EventHandler(Each_Tick); from Tick_Timer_Restart. The answer you accepted does not show a proper way to solve your problem.
    – Clemens
    Jul 31, 2015 at 9:24

4 Answers 4

3

Your Program will never work as expected! Everytime you click on your Start-Button, you will add a Tickhandler to the Dispatchertimer so you seconds start to increment by 2 then 3 and so on. You must remove the Handler in your Stop and Restart methods!!! Then all is working as expected and you don't have to create new DispatcherTimer in each Restart. By the way the solution of @DeshDeep Singh never carbage collect the old DispatcherTimer cause the EventHandler keeps it from being collected - so his solution brings you a big memory problem.

Here is the working code:

public int seconds;
public int minutes;
public int hours;

System.Windows.Threading.DispatcherTimer myTimer = new System.Windows.Threading.DispatcherTimer();

private void Tick_Timer_Start(object sender, RoutedEventArgs e)
{
    myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
    myTimer.Tick += Each_Tick;
    myTimer.Start();
}

private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
{
    myTimer.Tick -= Each_Tick;
    myTimer.Stop();
}

private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
{
    myTimer.Tick -= Each_Tick;
    seconds = 0;
    minutes = 0;
    Timertext.Text = "0 minutes : 0 seconds";
    myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
    myTimer.Tick += new EventHandler(Each_Tick);
    myTimer.Start();
}

private void Each_Tick(object sender, EventArgs e)
{
    seconds = seconds + 1;
    if (seconds < 10 && minutes < 10)
    {
        Timertext.Text = minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
    }
    else if (seconds > 59)
    {
        minutes = minutes + 1;
        seconds = 0;
        seconds = seconds + 1;
        Timertext.Text = minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
    }
    else
    {
        Timertext.Text = minutes.ToString() + " minutes : " + seconds.ToString() + " seconds";
    }
}
3
  • Not necessary to remove and re-attch the Tick handler each time, as it is the same method. Just add it once before the first start, then never change it again.
    – Clemens
    Jul 31, 2015 at 9:13
  • Yes, that's even better! Jul 31, 2015 at 11:03
  • You're right thank you to both you and @Clemens for your answers. I realise how the previous solution wasn't solving my problem
    – Naaz
    Aug 3, 2015 at 3:40
2

All you need to do is to attach the Tick handler only once:

private int seconds;
private int minutes;
private int hours;
private DispatcherTimer myTimer = new DispatcherTimer();

private void Initialize()
{
    myTimer.Interval = TimeSpan.FromMilliseconds(1000);
    myTimer.Tick += Each_Tick;
}

private void Tick_Timer_Start(object sender, RoutedEventArgs e)
{
    myTimer.Start();
}

private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
}

private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
    seconds = 0;
    minutes = 0;
    Timertext.Text = "0 minutes : 0 seconds";
    myTimer.Start();
}

private void Each_Tick(object o, EventArgs sender)
{
    ...
}

Even simpler would be a solution where you would only have Start and Stop, without any need for a Restart:

private int seconds;
private int minutes;
private int hours;
private DispatcherTimer myTimer = new DispatcherTimer();

private void Initialize()
{
    myTimer.Interval = TimeSpan.FromMilliseconds(1000);
    myTimer.Tick += Each_Tick;
}

private void Tick_Timer_Start(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
    seconds = 0;
    minutes = 0;
    Timertext.Text = "0 minutes : 0 seconds";
    myTimer.Start();
}

private void Tick_Timer_Stop(object sender, RoutedEventArgs e)
{
    myTimer.Stop();
}

private void Each_Tick(object o, EventArgs sender)
{
    ...
}
1

since the timer is not reset, do this:

private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
    {
        myTimer = new System.Windows.Threading.DispatcherTimer();

        seconds = 0;
        minutes = 0;
        Timertext.Text = "0 minutes : 0 seconds";
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();

    }
4
  • It works! Thank you so much. I'm so silly to have not tried that >.<
    – Naaz
    Jul 31, 2015 at 6:29
  • why you create new instance of timer each time when Restart is click ?
    – Bhavin
    Jul 31, 2015 at 6:31
  • since new instance of timer will bring all the values to 0, and you can use it as the way you want to. Jul 31, 2015 at 6:38
  • 2
    The values of seconds and minutes have nothing to do with the DispatcherTimer, so creating a new instance of a DispatcherTimer doesn't reset the values of seconds and minutes! You solution is causing a memory issue, cause you never remove the TickHandlers from the old DispatcherTimer, so it hangs in memory. It's absolutely unnecessary to create a new Timer here - see my solution. @naaz - I would appreciate it if you change the correct answer. Jul 31, 2015 at 8:14
0
     // put   myTimer.Stop() in Restart Method

   private void Tick_Timer_Restart(object sender, RoutedEventArgs e)
    {
        myTimer.Stop();  // stop timmer here and try
        seconds = 0;
        minutes = 0;
        Timertext.Text = "0 minutes : 0 seconds";
        myTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        myTimer.Tick += new EventHandler(Each_Tick);
        myTimer.Start();

    }
2
  • 1
    This didn't work, it still does the same thing as before. The above answer fixed my problem.
    – Naaz
    Jul 31, 2015 at 6:32
  • okay... myTimer.Tick -= new EventHandler(Each_Tick); also you can use remove event ..
    – Bhavin
    Jul 31, 2015 at 6:34

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.