5 minute count down timer

Diamonddrake 1 Tallied Votes 3K Views Share

I needed a 5 minute countdown for a project I was working on so I hacked this up, It does nothing more and nothing less. Figured I would post it while I was here to save someone else the trouble. (Not that it's in anyway difficult to do.)

ddanbe commented: Nice! +6
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace CountDownTimers
{
    public class Fivemintimer
    {
        Timer T = new Timer();

        int Minutes = 5;
        int Seconds = 0;

        public Fivemintimer()
        {
            T.Interval = 1000;
            T.Tick += new EventHandler(T_Tick);
        }

        public delegate void onTimerFinishedDelegate();
        public event onTimerFinishedDelegate onTimerFinished;

        public delegate void onTimerChanged(FIVETimerEventArgs args);
        public event onTimerChanged onTimerTick;

        void T_Tick(object sender, EventArgs e)
        {
            if (Minutes == 0 && Seconds == 0)
            {
                T.Stop();

                if (onTimerFinished != null)
                {
                    onTimerFinished();
                }
            }
            else
            {

                if (Seconds == 0)
                {
                    Seconds = 59;

                    Minutes--;
                }
                else
                {
                    Seconds--;
                }

            }

            if (onTimerTick != null)
            {
                onTimerTick(new FIVETimerEventArgs(Minutes, Seconds));
            }

        }

        public void PlayPause()
        {
            if (T.Enabled)
            {
                T.Stop();
            }
            else
            {
                T.Start();
            }
        }

        public void Reset()
        {

            Minutes = 5;
            Seconds = 0;
        }

        public bool Enabled
        {
            get
            {
                return T.Enabled;
            }
        }


    }

    public class FIVETimerEventArgs : EventArgs
    {

        public string TimeLeft;

        public FIVETimerEventArgs(int min, int sec)
        {
            string S;
            if (sec < 10)
            {
                S = "0" + sec.ToString();
            }
            else
            {

                S = sec.ToString();
            }

            TimeLeft = min.ToString() + ":" + S;
        }

    }
}
ddanbe 2,724 Professional Procrastinator Featured Poster

Nice! It is possible to turn this into an egg timer!

anders.blomqvist2 0 Newbie Poster

Looks like this is perfect for my needs. Anyone knows how to implement this in a webform application

TrustyTony 888 pyMod Team Colleague Featured Poster

For another timing idea, see my work and pause time timer in Python language. Maybe anybody want to do similar thing in C#? Or you could transfer it to Iron Python for .Net environment.

kplcjl 17 Junior Poster

You'd think 1000 would accurately represent 1 second. I found that if I compared ticks to the starting tick value that my elapsed time timer would occationally jump 2 seconds using 1000. By the time 5 minutes elapsed, you'd probably be off by about 5 seconds using this minute second process. On a timer that is dependent on human reactions, this code is fine, it'll be at most 5 seconds slow and if you use the minute/second method used here to display your time, you'd never have a clue your time is at all off.
(Since I didn't use UTCNow, my timer could jump an hour ahead or backwards. Wasn't thinking of anyone playing with code at 2AM on two special days a year when I wrote it and didn't set up the timer to display hours so you'd see 60+ minutes going forward.)

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.