C# countdown timer

你。 提交于 2019-11-30 13:57:46

问题


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


回答1:


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.




回答2:


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();
}



回答3:


Your code sets up the variables then goes to sleep for 3 minutes so the if-statement isn't executed until it leaves the sleep state. Either set up a new thread to update the UI or do something like this...

while (DateTime.now < end) {
  label1.Text = "...";
  Thread.Sleep(#); //Pick a second, or 5 or whatever
}

label1.Text = "Done!";

With a 2nd thread you can still do stuff in your program while it works. "Done!" will appear once it finishes.




回答4:


You could also use a Timer, as this would handle all the problems like UI-locking. You can use the System.Windows.Forms.Timer-Timer. In the MSDN library you can find samples of the use of it.

The WinForms-Timer handles also the invoking across the Timer-thread and the UI-thread.

- SeriTools



来源:https://stackoverflow.com/questions/7970580/c-sharp-countdown-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!