试图在标签内显示当前时间HH:mm:ss.ff

时间:2014-05-20 16:08:05

标签: c# wpf

我试图在显示计数毫秒,秒,分钟和小时的标签内显示当前日期时间。我做了这个,但它显示有一些延迟。

如果我遗漏了一些明显的东西,请原谅我

Thread thUpdateTime = new Thread(() =>
{
    while(true)
    {
        this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
        {
            Thread.Sleep(100);
            txtTime.Text = DateTime.Now.ToString("HH:mm:ss.ff");
        }));
    }
});
thUpdateTime.Name = "some description";
thUpdateTime.Start();

1 个答案:

答案 0 :(得分:2)

您可以使用DispatcherTimer:

var timer = new DispatcherTimer();
timer.Tick += (s, e) => txtTime.Text = DateTime.Now.ToString("HH:mm:ss.fff");
timer.Interval = TimeSpan.FromSeconds(0.1);
timer.Start();
相关问题