https://github.com/soenneker/soenneker.utils.backgroundqueue
A high-performance background Task/ValueTask queue
https://github.com/soenneker/soenneker.utils.backgroundqueue
background csharp dotnet queue task valuetask
Last synced: about 2 months ago
JSON representation
A high-performance background Task/ValueTask queue
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.utils.backgroundqueue
- Owner: soenneker
- License: mit
- Created: 2023-03-19T22:17:28.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-09-16T21:49:37.000Z (2 months ago)
- Last Synced: 2025-09-16T22:07:25.641Z (2 months ago)
- Topics: background, csharp, dotnet, queue, task, valuetask
- Language: C#
- Homepage: https://soenneker.com
- Size: 2.61 MB
- Stars: 8
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
- anything_about_game - backgroundqueue
README
[](https://www.nuget.org/packages/Soenneker.Utils.BackgroundQueue/)
[](https://github.com/soenneker/soenneker.utils.backgroundqueue/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/Soenneker.Utils.BackgroundQueue/)
#  Soenneker.Utils.BackgroundQueue
### A high-performance background Task/ValueTask queue
## Overview
`BackgroundQueue` provides an efficient way to manage background task execution in .NET applications. It helps prevent application overload by processing tasks in a controlled, asynchronous manner.
## Features
- Supports both `ValueTask` and `Task` types.
- Configurable queue size to limit resource usage.
- Built-in tracking of running and pending tasks.
- Extension methods for easy setup and management.
- Includes a hosted service for automatic background processing.
## Installation
```sh
dotnet add package Soenneker.Utils.BackgroundQueue
```
Register the `BackgroundQueue`:
```csharp
void ConfigureServices(IServiceCollection services)
{
services.AddBackgroundQueueAsSingleton();
}
```
### Starting
```csharp
await serviceProvider.WarmupAndStartBackgroundQueue(cancellationToken);
```
For synchronous start:
```csharp
serviceProvider.WarmupAndStartBackgroundQueueSync(cancellationToken);
```
### Stopping
To stop the service:
```csharp
await serviceProvider.StopBackgroundQueue(cancellationToken);
```
For synchronous stop:
```csharp
serviceProvider.StopBackgroundQueueSync(cancellationToken);
```
## Configuration
Configure the queue length and task tracking settings in your application:
```json
{
"Background": {
"QueueLength": 5000,
"LockCounts": false,
"Log": false
}
}
```
- `QueueLength`: Defines the maximum number of tasks in the queue.
- `LockCounts`: Enables thread-safe counting of running tasks.
- `Log`: Outputs task tracking information to `ILogger`
## Initializing the Queue
To use `BackgroundQueue`, you probably want to inject it via your constructor:
```csharp
IBackgroundQueue _queue;
void MyClass(IBackgroundQueue queue)
{
_queue = queue;
}
```
## Queueing Tasks
### Queuing a `ValueTask`
Rather than wrapping the task, you can elide it directly to avoid an extra state machine:
```csharp
await _queue.QueueValueTask(_ => someValueTask(), cancellationToken);
```
### Queuing a `Task`
Similarly, for `Task`:
```csharp
await _queue.QueueTask(_ => someTask(), cancellationToken);
```
## Waiting for Queue to Empty
To ensure all queued tasks finish before proceeding:
```csharp
await queue.WaitUntilEmpty(cancellationToken);
```
## Task Tracking
The queue tracks:
- The number of active `ValueTask` and `Task` instances.
- Whether any tasks are still processing.
To check if tasks are running:
```csharp
bool isProcessing = await queueInformationUtil.IsProcessing(cancellationToken);
```
To get current task counts:
```csharp
var (taskCount, valueTaskCount) = await queueInformationUtil.GetCountsOfProcessing(cancellationToken);
```