hey guys
i have 2 timers class in my code
one timer tick every 1 min and the other timer tick every second to show elapsed time in timer 1
here is the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimerTest
{
    public partial class Form1 : Form
    {
        Timer t1 = new Timer(); // timer 1
        Timer t2 = new Timer(); // timer 2
        int timeInteger;

        public Form1()
        {
            InitializeComponent();
        }


        private void Start_Click(object sender, EventArgs e)
        {
            t1.Interval = 60000; // set timer one 1 minute
            t2.Interval = 1000; // set timer two 1 second
            timeInteger = t1.Interval; // set timer 1 in integer


            //Hook event handler
            t1.Tick += new EventHandler(t1_Tick);
            t2.Tick += new EventHandler(t2_tick);
            //start the timer
            t1.Start();
            t2.Start();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            timeInteger = 0;
            label1.Text = "";
            t1.Dispose();
            t2.Dispose();
        }

        void t2_tick(object sender, EventArgs e)
        {
            timeInteger -= 1000; // how many time left minus 1000 millisecond
            label1.Text = timeInteger.ToString(); // display the time
        }
        void t1_Tick(object sender, EventArgs e)
        {
            //timer counts 60000 milliseconds. :)
        }
    }
}

the code works fine when i click start
but when i click stop then start again the time displayed go completely wrong

whats the problem

Recommended Answers

All 2 Replies

You dispose of the objects in your stop method and never create new ones.

As Momerath said, and I would add only that you do not create a new insatance of a Timer class ones again when pressing a start button!
Try to change the code into:

public partial class Form1 : Form
    {
        Timer t1; // timer 1
        Timer t2; // timer 2
        int timeInteger;

        public Form1()
        {
            InitializeComponent();
        }


        private void Start_Click(object sender, EventArgs e)
        {
            t1 = new Timer();
            t2 = new Timer();
            t1.Interval = 60000; // set timer one 1 minute
            t2.Interval = 1000; // set timer two 1 second
            timeInteger = t1.Interval; // set timer 1 in integer


            //Hook event handler
            t1.Tick += new EventHandler(t1_Tick);
            t2.Tick += new EventHandler(t2_tick);
            //start the timer
            t1.Start();
            t2.Start();
        }
  
        //AND THE ADDITIONAL CODE BELLOW HERE...
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.