Pass async Callback to Timer constructor
Is there any way to omit that o param in lambda?
Sure, just define your event handler method as async void
:
private async void HandleTimerCallback(object state)
You could use a wrapper method, as recommended by David Fowler here:
public class Pinger
{
private readonly Timer _timer;
private readonly HttpClient _client;
public Pinger(HttpClient client)
{
_client = client;
_timer = new Timer(Heartbeat, null, 1000, 1000);
}
public void Heartbeat(object state)
{
// Discard the result
_ = DoAsyncPing();
}
private async Task DoAsyncPing()
{
await _client.GetAsync("http://mybackend/api/ping");
}
}