使用System.Threading.Timer显示错误的时间

时间:2014-03-06 16:09:19

标签: c# multithreading winforms clock

我是线程新手,不了解它应该如何使用。我的时钟有问题。我的时钟没有给我正确的当地时间,而是给了我大约10-15分钟的延迟时间。

我认为我的代码存在错误或遗漏:

private void MainWindow_Load(object sender, EventArgs e)
{
  new System.Threading.Timer((state) => 
  { 
    if (!label2.IsHandleCreated) 
      return; 
    BeginInvoke((Action)delegate() 
      { label2.Text = "    " + DateTime.Now.ToString("hh:MM:ss") + "  " +
                               DateTime.Now.ToShortDateString(); });
      }, null, 1000, 1000);
  }

我已经读过,begininvoke应该始终与endinvoke配对。但是我该怎么做呢?

编辑:我需要知道为什么这段代码给我延迟时间,就像现在它的12:23,但程序显示12:03,它总是从12:03开始,当我重新运行它。有时候时钟停止。为什么呢?

1 个答案:

答案 0 :(得分:1)

您的格式字符串hh:MM:ss显示小时(12小时制)::秒。您可能打算hh:mm:ss 有关详细说明,请参阅MSDN上的Custom Date and Time Format Strings

相关问题