I'm considering use Task.Delay()
for a non-stop timer, because it's more simple and readable.
As I'm new to .NET, I see no significant difference between the two codes. Can you show me the difference (if there is any) between them?
// 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
}
vs
// Every thing inside a function
async void TaskTimer()
{
while (true)
{
await Task.Delay(5000);
// Do stuff
}
}