I have a timer in a Windows Service, and there is a call made to an async method inside the timer_Elapsed event handler:
protected override void OnStart(string[] args)
{
timer.Start();
}
private async void timer_Elapsed(object sender, ElapsedEventArgs e)
{
_timer.Stop();
await DoSomething();
_timer.Start();
}
Is the above code ok? Firstly, I have read that async void is not a good idea. Secondly, why would I need the timer_Elapsed method to be async in the first place? It's not like the elapsed event handler us going to get called in parallel by multiple callers. However, if I don't make the method async, then my logic will break because timer will start before DoSomething completes.
So, is making timer_Elapsed async the correct approach?