Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

C#advancedbackendAI generated

Concurrent Task Scheduler Library

Build a reusable .NET library for running background tasks: one-off tasks, tasks that repeat every few minutes, and tasks on a daily schedule, each with a priority, with a limit on how many run at once. Tasks that fail are retried with increasing waits, tasks can be cancelled, and the scheduler shuts down cleanly, letting running tasks finish. It's built on async/await, CancellationToken and System.Threading.Channels, and comes with a demo console app showing it in action. You'll learn designing a library API, async programming in depth, priority queues, limiting concurrency with channels or SemaphoreSlim, cancellation, retries, and testing time-dependent code with a fake clock.

Estimate
~14h
Steps
7
Completed by
0
Proposed by
codeseed.app

.NET Task Parallel Library · System.Threading.Channels

Project roadmap

  1. 01

    Design the API

    ~2h

    Create a class library and a test project. Define the public API: an IScheduledTask interface (ExecuteAsync(CancellationToken)), schedules (Once(at), Every(interval) and DailyAt(time, timeZone)), options per task (priority, max retries, timeout), and a Scheduler class with Schedule, Cancel(taskId), and StartAsync and StopAsync. Use TimeProvider for the current time so tests can control it. Write README.md with usage examples. Done when: the API compiles, and README.md shows how it's used.

  2. 02

    Build the scheduling loop

    ~2.5h

    Keep scheduled tasks in a PriorityQueue ordered by next run time, then priority. A loop waits until the earliest task is due (using the TimeProvider's delay, and waking early when a new task is added), then moves due tasks to execution and computes each repeating task's next run time. Protect the queue so scheduling from other threads is safe. Done when: tasks run at the right times and repeating tasks keep repeating.

  3. 03

    Limit concurrency

    ~2h

    Send due tasks through a bounded Channel read by a fixed number of worker loops (the concurrency limit), so higher-priority tasks go first when several are due. Decide what happens when a repeating task is still running when it's due again (skip that run, and log it). Done when: with a limit of 2 and 5 due tasks, at most 2 run at once, highest priority first.

  4. 04

    Cancellation and shutdown

    ~2h

    Give each run a CancellationToken linked to the scheduler's shutdown token and the task's timeout. Cancel(taskId) removes a task and cancels its current run. StopAsync stops scheduling new runs, waits for running tasks up to a grace period, then cancels them. Done when: stopping waits for running tasks, and timeouts cancel slow tasks.

  5. 05

    Handle errors and retries

    ~1.5h

    Catch exceptions from tasks so one failure never stops the scheduler, log them with ILogger, and retry according to the task's options with exponential backoff and jitter. Raise events (or callbacks) for task started, succeeded, failed and retried, so users can monitor it. Done when: a task that fails twice then succeeds is retried with growing waits, and a crashing task doesn't affect others.

  6. 06

    Build the demo app

    ~1.5h

    Create a console app that registers several demo tasks (a fast frequent task, a slow one, one that fails randomly, and a daily one), prints a live log of task events, and stops cleanly on Ctrl+C. Done when: the demo shows scheduling, concurrency limits, retries and clean shutdown.

  7. 07

    Test with a fake clock

    ~2.5h

    Write xUnit tests using FakeTimeProvider (from Microsoft.Extensions.TimeProvider.Testing) to move time forward instantly: run times for each schedule type (including daily tasks across daylight saving changes), priority order, the concurrency limit, overlap skipping, cancellation, timeouts, retries and shutdown. Done when: dotnet test passes quickly and reliably.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~14h · 7 steps

Tech stack

.NET Task Parallel LibrarySystem.Threading.Channels