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.
Post History
Which concept is best for managed background services? 1. RunAsync(CancellationToken): interface IWorker { Task RunAsync(CancellationToken cancellationToken = default); } ... IWorker worker =...
#4: Post edited
Which concept is best for managed background services? ### 1. `RunAsync(CancellationToken)`: ```csharp 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` ```csharp interface IWorker { Task RunAsync(); Task StopAsync(); } ... IWorker worker = ...; _ = worker.RunAsync(); // And when it's time to shutdown the worker: await worker.StopAsync(); ```
#3: Post edited
- Which concept is best for managed background services?
1. `RunAsync(CancellationToken)`:- ```csharp
- 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`- ```csharp
- interface IWorker
- {
- Task RunAsync();
- Task StopAsync();
- }
- ...
- IWorker worker = ...;
- _ = worker.RunAsync();
- // And when it's time to shutdown the worker:
- await worker.StopAsync();
- ```
- Which concept is best for managed background services?
- ### 1. `RunAsync(CancellationToken)`:
- ```csharp
- 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`
- ```csharp
- interface IWorker
- {
- Task RunAsync();
- Task StopAsync();
- }
- ...
- IWorker worker = ...;
- _ = worker.RunAsync();
- // And when it's time to shutdown the worker:
- await worker.StopAsync();
- ```
#2: Post edited
- Which concept is best for managed background services?
1) `RunAsync(CancellationToken)`:- ```csharp
- 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`- ```csharp
- interface IWorker
- {
- Task RunAsync();
- Task StopAsync();
- }
- ...
- IWorker worker = ...;
- _ = worker.RunAsync();
- // And when it's time to shutdown the worker:
- await worker.StopAsync();
- ```
- Which concept is best for managed background services?
- 1. `RunAsync(CancellationToken)`:
- ```csharp
- 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`
- ```csharp
- interface IWorker
- {
- Task RunAsync();
- Task StopAsync();
- }
- ...
- IWorker worker = ...;
- _ = worker.RunAsync();
- // And when it's time to shutdown the worker:
- await worker.StopAsync();
- ```
#1: Initial revision
Which abstraction should I choose for background services and why?
Which concept is best for managed background services? 1) `RunAsync(CancellationToken)`: ```csharp 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` ```csharp interface IWorker { Task RunAsync(); Task StopAsync(); } ... IWorker worker = ...; _ = worker.RunAsync(); // And when it's time to shutdown the worker: await worker.StopAsync(); ```