C# countdown timer

The Mask picture The Mask · Nov 1, 2011 · Viewed 39.5k times · Source

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

Answer

CodesInChaos picture CodesInChaos · Nov 1, 2011

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.