Skip to content

Instantly share code, notes, and snippets.

@burakcanekici
Last active November 2, 2020 07:22
Show Gist options
  • Save burakcanekici/cde9bb3bd0651f1addc90b70c8e6d96f to your computer and use it in GitHub Desktop.
Save burakcanekici/cde9bb3bd0651f1addc90b70c8e6d96f to your computer and use it in GitHub Desktop.
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);
}
@burakcanekici
Copy link
Author

hello

@burakcanekici
Copy link
Author

~be

@burakcanekici
Copy link
Author

~be

@burakcanekici
Copy link
Author

~be

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment