Created
September 28, 2020 16:46
-
-
Save jean-lourenco/c4d541556b147661ef458c80b6b464df to your computer and use it in GitHub Desktop.
IHostedService com Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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