Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# countdown timer

I'm trying to make a countdown using C# and show the time in format:

hour:minutes:seconds

I've tried this:

 var minutes = 3; //countdown time
  var start = DateTime.Now;
  var end = DateTime.Now.AddMinutes(minutes);
  Thread.Sleep(1800);
  if (??) // I tried DateTime.Now > end not works
  {
       //... show time
      label1.Text = "..."; 
  } 
  else 
  {
     //done 
      label1.Text = "Done!"; 
  }

Different ways to solve this problem also appeared. Thanks in advance

like image 386
The Mask Avatar asked Nov 01 '11 17:11

The Mask


2 Answers

You should not use Thread.Sleep here. Thread.Sleep on the UI thread blocks the UI, and using it on another thread leads to additional complexity due to thread synchronization.

If you have C# 5 or the async CTP you probably can write code very similar to what you did, since you then get a continuation based equivalent of Thread.Sleep that doesn't block the UI.

In standard C# 4 I'd use a System.Windows.Forms.Timer.

To start the countdown:

var minutes = 3; //countdown time
var start = DateTime.UtcNow; // Use UtcNow instead of Now
endTime = start.AddMinutes(minutes); //endTime is a member, not a local variable
timer1.Enabled = true;

In the timer handler you write:

TimeSpan remainingTime=endTime-DateTime.UtcNow;
if(remainingTime<TimeSpan.Zero)
{
   label1.Text = "Done!";
   timer1.Enabled=false; 
}
else
{
  label1.Text = remainingTime.ToString();
}

For other formatting options see Standard TimeSpan Format Strings.

One issue that remains with this code is that it will not work correctly if the system clock changes.

When using DateTime.Now instead of DateTime.UtcNow it will also break when switching from/to daylight saving or changing the timezone. Since you want to identify a certain point in time (and not a display time) you should use UTC instead of local time.

like image 79
CodesInChaos Avatar answered Sep 25 '22 13:09

CodesInChaos


I would use a timer something like this. First a couple of instance variables.

private int _countDown = 30; // Seconds
private Timer _timer;

and in the constructor or load event

_timer = new Timer();
_timer.Tick += new EventHandler(timer_Tick);
_timer.Interval = 1000;
_timer.Start();

and then finally the event handler

void timer_Tick(object sender, EventArgs e)
{
    _countDown--;
    if (_countDown < 1)
    {
        _countDown = 30;
    }
    lblCountDown.Text = _countDown.ToString();
}
like image 30
Jonas Elfström Avatar answered Sep 25 '22 13:09

Jonas Elfström