Skip to content

Instantly share code, notes, and snippets.

@jean-lourenco
Created September 28, 2020 16:46
Show Gist options
  • Save jean-lourenco/c4d541556b147661ef458c80b6b464df to your computer and use it in GitHub Desktop.
Save jean-lourenco/c4d541556b147661ef458c80b6b464df to your computer and use it in GitHub Desktop.
IHostedService com Timer
public class ContractUpdated : IHostedService, IDisposible
{
private Timer _timer;
public Task StartAsync(CancellationToken token)
{
_timer = new Timer(
UpdateContracts,
null,
TimeSpan.Zero,
TimeSpan.FromMinutes(60))
return Task.CompletedTask;
}
private void UpdateContracts(object state)
{
// Executando um código muito demorado
Thread.Sleep(5000);
}
public Task StopAsync(CancellationToken token)
{
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment