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
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With