English 中文(简体)
C 减记时间
原标题:C# countdown timer

Im试图利用C#进行缩编,并用格式显示时间:

hour:minutes:seconds

我试图这样做:

 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!"; 
  }

解决这一问题的方法也不尽相同。 预 收

最佳回答

您不应在此使用<条码>。 Thread.Sleep on the UI thread Band the UI, and using it on another thread causes to increase details due to read coordinatedation.

如果你有C# 5或CTP,那么你或许可以写出与你所写的非常相似的代码,因为你可以继续使用相当于<条码>的编号。

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;

在时间手里,你写:

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

其他格式选择见。 Standard TimeSpan Format Strings

该法典还有一个问题,即如果系统锁定变化,它不会正确发挥作用。

使用<代码> 现在,,而不是Datetime.UtcNow,在从/到日光储蓄或改变时间区时,也会中断。 由于你想要确定某个时间点(而不是一个显示时间),你应当使用UTC而不是当地时间。

问题回答

我会利用这样的时间。 首先是两种情况变数。

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

和在施工或装货活动中

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

接着是活动手稿

void timer_Tick(object sender, EventArgs e)
{
    _countDown--;
    if (_countDown < 1)
    {
        _countDown = 30;
    }
    lblCountDown.Text = _countDown.ToString();
}

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.

WinForms-timer还负责全时和全时地援引。

- SeriTools

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!";

有了两条read子,在你的工作过程中,你仍然能够 st。 “Done>一经结束即出现。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签