Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Which abstraction should I choose for background services and why?
Post
Which abstraction should I choose for background services and why?
+6
−1
Which concept is best for managed background services?
1. RunAsync(CancellationToken)
:
interface IWorker
{
Task RunAsync(CancellationToken cancellationToken = default);
}
...
IWorker worker = ...;
var source = new CancellationTokenSource();
_ = worker.RunAsync(source.Token);
// And when it's time to shutdown the worker:
source.Cancel();
vs
2. RunAsync
and StopAsync
interface IWorker
{
Task RunAsync();
Task StopAsync();
}
...
IWorker worker = ...;
_ = worker.RunAsync();
// And when it's time to shutdown the worker:
await worker.StopAsync();
1 comment thread