Round #723
10 May 2026, 15:14· csharp· Meta-Llama-3.3-70B-Instruct· r13-clean-hints
- Complexity
- band 0
- ZP / UO / OBS
- 1 / 2 / 1
- Journey weight
- 237
- Lightshift ΔL
- +0.00
Prompt
Build a polling worker as BackgroundService that uses PeriodicTimer, respects IHostApplicationLifetime.ApplicationStopping, and emits Activity tags.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0022 | 2026-05-10T15:14:25Z
Build a polling worker as BackgroundService that uses PeriodicTimer, respects IHostApplicationLifetime.ApplicationStopping, and emits Activity tags.
ZERO POINT — 1 entries (structural invariants)
● hsl(61.72, 100%, 50%) BackgroundService [contract]
UNOBSERVED — 2 entries (implied truths)
○ hsl(27.69, 100%, 50%) async [feature]
○ hsl(3.75, 100%, 38%) CancellationToken [feature]
OBSERVED — 1 entries (explicit in request)
◉ hsl(232.62, 100%, 25%) BackgroundService [function]
COMPLEXITY │ 4 entries │ spread: 229° │ band: LOW
Baseline response (no ZPI)
Here's an example implementation of a polling worker as a `BackgroundService` that uses `PeriodicTimer`, respects `IHostApplicationLifetime.ApplicationStopping`, and emits Activity tags:
```csharp
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
public class PollingWorker : BackgroundService
{
private readonly ILogger<PollingWorker> _logger;
private readonly IHostApplicationLifetime _lifetime;
public PollingWorker(ILogger<PollingWorker> logger, IHostApplicationLifetime lifetime)
{
_logger = logger;
_lifetime = lifetime;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var activitySource = new ActivitySource("PollingWorker");
var activity = activitySource.StartActivity("Polling", ActivityKind.Internal);
try
{
var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
while (await timer.WaitForNextTickAsync(stoppingToken) && !_lifetime.ApplicationStopping.IsCancellationRequested)
{
using (activitySource.StartActivity("Poll", ActivityKind.Internal))
{
// Perform polling operation here
_logger.LogInformation("Polling operation executed");
}
}
}
catch (OperationCanceledException)
{
_logger.LogInformation("Polling worker stopped due to cancellation");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred in polling worker");
}
finally
{
activity?.Stop();
}
}
}
```
In this example:
* We create a `BackgroundService` called `PollingWorker` that takes an `ILogger` and an `IHostApplicationLifetime` in its constructor.
* In the `ExecuteAsync` method, we start an Activity using the `ActivitySource` class to track the execution of the polling worker.
* We create a `PeriodicTimer` that ticks every 10 seconds and use it to execute the polling operation in a loop.
* Inside the loop, we start a new Activity for each polling operation and log a message to indicate that the operation has been executed.
* We respect the `IHostApplicationLifetime.ApplicationStopping` token to stop the polling worker when the application is shutting down.
* We handle exceptions and cancellation, logging any errors that occur and stopping the Activity when the polling worker is stopped.
To use this `PollingWorker` in your application, you'll need to add it to the DI container in the `Program.cs` file:
```csharp
builder.Services.AddHostedService<PollingWorker>();
```
This will start the `PollingWorker` when the application starts, and it will continue to run in the background until the application is stopped.
Enhanced response (with ZPI)
```csharp
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
public class PollingWorker : BackgroundService
{
private readonly ILogger<PollingWorker> _logger;
private readonly IHostApplicationLifetime _lifetime;
public PollingWorker(ILogger<PollingWorker> logger, IHostApplicationLifetime lifetime)
{
_logger = logger;
_lifetime = lifetime;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
while (await timer.WaitForNextTickAsync(stoppingToken) && !_lifetime.ApplicationStopping.IsCancellationRequested)
{
using var activity = new Activity("PollingWorker").Start();
try
{
// Simulate some work
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
_logger.LogInformation("Polling worker executed successfully.");
}
catch (OperationCanceledException)
{
_logger.LogInformation("Polling worker was cancelled.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error executing polling worker.");
}
}
}
}
```
This implementation of `PollingWorker` as a `BackgroundService` utilizes `PeriodicTimer` to execute a task at regular intervals. The `IHostApplicationLifetime.ApplicationStopping` token is respected to ensure the worker stops when the application is shutting down.
The `Activity` class from the `System.Diagnostics` namespace is used to create an activity that represents the execution of the polling worker. This allows for the emission of activity tags, which can be useful for distributed tracing and logging.
In the `ExecuteAsync` method, a `PeriodicTimer` is created with a period of 10 seconds. The method then enters a loop where it waits for the next tick of the timer or until the `stoppingToken` is cancelled. If the application is stopping, the loop exits.
Inside the loop, an activity is started and some work is simulated using `Task.Delay`. If the work completes successfully, a log message is written. If the work is cancelled, a log message is written indicating that the worker was cancelled. If an exception occurs, a log message is written with the exception details.
The `PollingWorker` class can be registered in the DI container in the `Program.cs` file like this:
```csharp
builder.Services.AddHostedService<PollingWorker>();
```
This will start the `PollingWorker` when the application starts and stop it when the application is shutting down.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1samba/Meta-Llama-3.3-70B-Instruct
WorseV4qwen2.5-coder-3b-instruct
BetterV5cerebras/qwen-3-235b-a22b-instruct-2507
WorseV7qwen2.5-coder-7b-instruct
SameV8openai/gpt-4.1
WorseV9anthropic/claude-opus-4-7
WorseV12openai/gpt-4o
SameComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.