Hello,

I have a timer that starts after clicking a button, then a label is updated every tick event simulating the seconds, the problem is very weird, after one button click the timer starts so basically the label should start changing, but it does not.

Here is the weird thing, I need to click another button which has no code related to the timer in order to make the timer start.

It is like the form is waiting for something to happen for the timer to start even if I coded the timer to start after a previous different button event.

Thanks

You need to use delegate, which would check if the label needs invoke.

Take a look at this example I made for you:

public partial class Form1 : Form
    {
        delegate void MyDelegate(string msg);
        Timer timer1;
        int counter;

        public Form1()
        {
            InitializeComponent();
            label1.Text = "countier: 0";
            CreatingTimer();
        }

        private void CreatingTimer()
        {
            timer1 = new Timer();
            timer1.Tick+= new EventHandler(timer1_Tick);
            timer1.Interval = 1000;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++;
            timer1.Enabled = true;
            UpdateLabel(counter.ToString());           
        }

        private void UpdateLabel(string value)
        {
            if (this.label1.InvokeRequired)
                this.label1.Invoke(new MyDelegate(UpdateLabel), new object[] { value });
            else
                label1.Text = "counter: " + value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }

Ok, I think is kind of what I have done, please tell me if there is something wrong in my code or I have to add something.

int iTime = 0;
 
        private void timer_Tick(object sender, EventArgs e)
        {
            iTime++;
            klTimer.Text = iTime.ToString();
        }

I add the timer through Visual Studio interface and change the property of enable to false.

Then when I click the button I call the timer method: Start.

So basically the button event is

private void button1_Click(object sender, EventArgs e)
      {
        timer.Start();
      }

It is very strange because, the timer actually starts but the label starts updating just when I click any other button in the form, and it actually holds the number as if it was working the whole time.

Thanks

Code looks fine - it should work. I just dont know how do you create the timer. Create it like I do in the code, so it creats in the run time. This code of mine works:

public partial class Form1 : Form
    {
        Timer timer1;
        int counter;

        public Form1()
        {
            InitializeComponent();
            label1.Text = "countier: 0";
            CreatingTimer();
        }

        private void CreatingTimer()
        {
            timer1 = new Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++;
            timer1.Enabled = true;
            label1.Text = "counter: " + counter.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }

Solved.

I actually changed the property enabled instead of calling the method start like you did in your example and it worked.

Thanks

yee, but you didnt have that line of code in your upper example.
So, glad to hear its working.
Please if you are satisfied with the answer, just mark the thread as answered, so everyone who looking for a similar solution, can get it.
bye, bye
Mitja

Yes I did not have the line, your example helped me using that line instead of calling the method.

Thanks

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.