Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,

I want to execute a timer and update the timer value to a label on the UI. I tried something like this.

C#
int iCounter;
int iInterval = 10;
System.Timers.Timer t1 = null;

private void btnStart_Click(object sender, EventArgs e)
{
    iCounter = 6000;

    t1 = new System.Timers.Timer();
    t1.Interval = iInterval;
    t1.Elapsed += new ElapsedEventHandler(t1_Tick);
    t1.Enabled = true;
}

private void t1_Tick(object sender, EventArgs e)
{
    if (iCounter > 0)
    {
        if (lblTimer.InvokeRequired)
        {
            lblTimer.Invoke(new MethodInvoker(delegate
            {
                lblTimer.Text = iCounter.ToString();
            }));
        }
    }
    else
    {
        if (lblTimer.InvokeRequired)
        {
            lblTimer.Invoke(new MethodInvoker(delegate
            {
                lblTimer.Text = "0";
            }));
        }
        t1.Enabled = false;
    }

    iCounter = iCounter - iInterval;
}


Say I set the interval (timer interval) to 1000 milliseconds, the timer elapsed 6 seconds and completed. If I set the interval to a small value like 10 milliseconds it takes more than 6 seconds to complete.

Anyone can please comment on this, why there are two behaviors? Thanks in advance.
Posted

1 solution

The variation in timing may be due to garbage collection happening; read the commented code in the 'Main method here: [^]. Use of the 'GC.KeepAlive(YourTimerName) may have an impact on this variation.

In any case you can simplify your code by setting the 'Synchronization object-property of the Timer. Try this:
C#
using System;
using System.Timers;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        private int iCounter = 6000;
        private int iInterval = 10;

        private System.Timers.Timer t1 = new System.Timers.Timer();

        private void Form1_Load(object sender, EventArgs e)
        {
            t1.Interval = iInterval;
            t1.Elapsed += t1_Elapsed;

            // allow easy UI updates
            t1.SynchronizingObject = this;

            lblTimer.Text = iCounter.ToString();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            // avoid garbage collection
            GC.KeepAlive(t1);
            t1.Enabled = true;
        }

        private void t1_Elapsed(object sender, ElapsedEventArgs e)
        {
            lblTimer.Text = (iCounter -= iInterval).ToString();

            if (iCounter <= 0)
            {
                t1.Stop();
                lblTimer.Text += " : Stopped";
            }
        }
    }
}
I suggest you experiment with running the code with, and without, the call to 'GC.KeepAlive(t1) and see if you observe any run-time differences.
 
Share this answer
 
Comments
CodingLover 11-Mar-14 4:29am    
Thanks for the comment :)

I tried what you've explained above. But I didn't see a difference of time elapsed still, smaller values of interval leads to a considerable time period.
CodingLover 11-Mar-14 4:47am    
Anyway, I found an article about this from here too. http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer

It explains everything where I am.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900