Last active
November 2, 2020 07:22
-
-
Save burakcanekici/cde9bb3bd0651f1addc90b70c8e6d96f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 abstract class HostedService : IHostedService | |
{ | |
private Task _executingTask; | |
private CancellationTokenSource _cts; | |
private object serviceProvider; | |
protected HostedService(object serviceProvider) | |
{ | |
this.serviceProvider = serviceProvider; | |
} | |
public Task StartAsync(CancellationToken cancellationToken) | |
{ | |
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | |
_executingTask = ExecuteAsync(_cts.Token); | |
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask; | |
} | |
public async Task StopAsync(CancellationToken cancellationToken) | |
{ | |
if(_executingTask == null) | |
{ | |
return; | |
} | |
_cts.Cancel(); | |
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken)); | |
cancellationToken.ThrowIfCancellationRequested(); | |
} | |
public abstract Task ExecuteAsync(CancellationToken cancellationToken); | |
} |
~be
~be
~be
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello