user2871190
user2871190

Reputation: 251

Display time in seconds by using timer

I have a timer with an interval 1. Every time it ticks, i wanna add the time and display it on my form.

But something is wrong with it. Te time updates itself way to slow. If I set the interval to 1000, it works, but I need it to run faster.

Here is my code:

            private void button1_Click_1(object sender, EventArgs e)
    {
        timer.Interval = 1;
        timer.Start();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        lCount++;
        label1.Text = GetTimeForGUI(lCount);
    }

private String GetTimeForGUI(long lTimeInMilliSeconds)
    {
        String sGUITime = string.Empty;
        try
        {
            // Get Format: 00:00
            if (((lTimeInMilliSeconds / 1000) % 60) == 0)
            {
                sGUITime = Convert.ToString((lTimeInMilliSeconds / 1000) / 60);

                // Get the number of digits
                switch (sGUITime.Length)
                {
                    case 0:
                        sGUITime = "00:00";
                        break;   
                    case 1:
                        sGUITime = "0" + sGUITime + ":" + "00";
                        break;
                    case 2:
                        sGUITime = sGUITime + ":" + "00";
                        break;
                }
            }
            else
            {
                long lMinutes;
                long lSeconds;

                // Get seconds
                lTimeInMilliSeconds = lTimeInMilliSeconds / 1000;
                lSeconds = lTimeInMilliSeconds % 60;
                lMinutes = (lTimeInMilliSeconds - lSeconds) / 60;

                switch (Convert.ToString(lMinutes).Length)
                {
                    case 0:
                        sGUITime = "00";
                        break;
                    case 1:
                        sGUITime = "0" + Convert.ToString(lMinutes);
                        break;
                    case 2:
                        sGUITime = Convert.ToString(lMinutes);
                        break;    
                }

                sGUITime = sGUITime + ":";

                switch (Convert.ToString(iSeconds).Length)
                {
                    case 0:
                        sGUITime = sGUITime + "00";
                        break;
                    case 1:
                        sGUITime = sGUITime + "0" + Convert.ToString(lSeconds);
                        break;
                    case 2:
                        sGUITime = sGUITime + Convert.ToString(lSeconds);
                        break;
                }

            }

            return sGUITime;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return string.Empty;
        }
    }

Upvotes: 2

Views: 6186

Answers (3)

Marvin Smit
Marvin Smit

Reputation: 4108

I think you are trying to do too much yourself. Use the framework to help you.

Don't caluclate your own seconds based on a counter when you have a Timer and DateTime available.

The implementation below will give you your seconds in the label (updated every 100 milliseconds)

    DateTime startTime;
    private void button1_Click(object sender, EventArgs e)
    {
        timer.Tick += (s, ev) => { label1.Text = String.Format("{0:00}", (DateTime.Now - startTime).Seconds); };
        startTime = DateTime.Now;
        timer.Interval = 100;       // every 1/10 of a second
        timer.Start();
    }   

Hope this helps,

Upvotes: 2

Ondrej Tucny
Ondrej Tucny

Reputation: 27962

// Get seconds
lTimeInMilliSeconds = lTimeInMilliSeconds / 1000;
lSeconds = lTimeInMilliSeconds % 60;
lMinutes = (lTimeInMilliSeconds - lSeconds) / 60;

As long as you live on planet Earth and use the SI, there are 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day.

If basic math is not your favorite, you should cosider using DateTime and TimeSpan.

Upvotes: 0

Ehsan
Ehsan

Reputation: 32681

1 means 1 millisecond. and by the time you are done with GUI updation it would have fired again. This is the reason you are facing delay. I can't figure out why on earth do you want to update every 1 millisecond. You need to post your requirements clearly in order to get a better answer.

Upvotes: 0

Related Questions