user2184248
user2184248

Reputation: 27

C# Countdown Timer Then Do Something

Looked everywhere, but everywhere i look theres a different way to do a countdown timer. Finally found some simple code. How do I make it do something when the time is complete.

This part is next to InitializeComponent();

timerlabel1.Text = TimeSpan.FromMinutes(720).ToString();

private void countdownTimer()
{
    var startTime = DateTime.Now;

    var timer = new Timer() { Interval = 1000 };

    timer.Tick += (obj, args) =>
        timerlabel1.Text =
            (TimeSpan.FromMinutes(720) - (DateTime.Now - startTime))
            .ToString("hh\\:mm\\:ss");

    timer.Enabled = true;
}

This is where i need help, how do i make it do something when the time is done. I tried if timer.Enabled =false; Do This. Cant figure it out.

Upvotes: 1

Views: 10186

Answers (4)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Solution : you can assign the total seconds [TotalMinutes*60] into some variable and decrement each time the Timer Tick event raises.

if the totalseconds value becomes zero then stop the timer by calling timer.Stop() method.

Try This:

       public int tootalsecs = 720 * 60;
       private void countdownTimer()
       {
         var startTime = DateTime.Now;

         var timer = new Timer() { Interval = 1000 };

         timer.Tick += (obj, args) =>
         {
            if (tootalsecs==0)
            {
                timer.Stop();
            }
            else
            {
                timerlabel1.Text =
               (TimeSpan.FromMinutes(720) - (DateTime.Now - startTime))
                   .ToString("hh\\:mm\\:ss");
                tootalsecs--;
            }
        };
             timer.Start();
        }

Upvotes: 2

Ashok
Ashok

Reputation: 1906

You can try to do as below.

Initialize in global scope.

var target;
timerlabel1.Text =target= DateTime.Now.Add(TimeSpan.FromMinutes(720));

Add a Timer and in timer1_Tick write the below code

var span = targetTime - DateTime.Now;

 if (span.TotalSeconds > 0)
  {
//it will continue till the time ends. 
    var temp = span.ToString();
    temp=temp.Substring(0, 8);
    timerlabel1.Text = temp;
  }
 else
//do your work here

Don't forget to validate the answer or mark as answer if it close to your need

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

As there are couple Timer classes available (System.Windows.Forms.Timer, System.Threading.Timer and System.Timers.Timer) I will advice you to go with System.Timers.Timer.

It provides Elapsed event, instead of Tick event. That's what you're looking for.

// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;

Upvotes: 0

Neel Bhasin
Neel Bhasin

Reputation: 759

try this may work for you

var timer=new Timer();
timer.Interval=1000;
timer.tick += timer_Tick;
timer.Start();
int i=0;

void timer_Tick(object sender, EventArgs e)
{
if(i<TimeSpan.FromMinutes(720))
{
 timerlabel1.Text =
            (TimeSpan.FromMinutes(720) - (DateTime.Now - startTime))
            .ToString("hh\\:mm\\:ss");
}
else
{
timer.Stop();
/* do other work Here */
}
i++;
}

Try with this may work for you.

Upvotes: 0

Related Questions