我正在考虑使用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
}
}