【发布时间】:2014-01-31 00:15:49
【问题描述】:
我正在考虑将Task.Delay() 用于不间断计时器,因为它更简单易读。
由于我是 .NET 新手,我认为这两个代码之间没有显着差异。你能告诉我它们之间的区别(如果有的话)吗?
// Create variable at some place
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += timer_Elapsed;
timer.Start();
// Function other place
void timer_Elapsed(object sender, EventArgs e)
{
//Do stuff
}
对
// Every thing inside a function
async void TaskTimer()
{
while (true)
{
await Task.Delay(5000);
// Do stuff
}
}
【问题讨论】:
-
从哪里开始TaskTimer?
标签: c# task async-await delay dispatchertimer