{"id":24271875,"url":"https://github.com/soenneker/soenneker.utils.backgroundqueue","last_synced_at":"2026-04-21T22:07:05.794Z","repository":{"id":144719585,"uuid":"616201575","full_name":"soenneker/soenneker.utils.backgroundqueue","owner":"soenneker","description":"A high-performance background Task/ValueTask queue","archived":false,"fork":false,"pushed_at":"2026-03-31T13:47:12.000Z","size":3098,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2026-03-31T15:32:23.052Z","etag":null,"topics":["background","csharp","dotnet","queue","task","valuetask"],"latest_commit_sha":null,"homepage":"https://soenneker.com","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soenneker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"soenneker","thanks_dev":"soenneker"}},"created_at":"2023-03-19T22:17:28.000Z","updated_at":"2026-03-31T13:46:48.000Z","dependencies_parsed_at":"2026-01-29T00:04:54.420Z","dependency_job_id":null,"html_url":"https://github.com/soenneker/soenneker.utils.backgroundqueue","commit_stats":null,"previous_names":[],"tags_count":224,"template":false,"template_full_name":null,"purl":"pkg:github/soenneker/soenneker.utils.backgroundqueue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.backgroundqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.backgroundqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.backgroundqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.backgroundqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soenneker","download_url":"https://codeload.github.com/soenneker/soenneker.utils.backgroundqueue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.backgroundqueue/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31290977,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T13:12:26.723Z","status":"ssl_error","status_checked_at":"2026-04-01T13:12:25.102Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background","csharp","dotnet","queue","task","valuetask"],"created_at":"2025-01-15T17:59:51.983Z","updated_at":"2026-04-01T18:54:40.094Z","avatar_url":"https://github.com/soenneker.png","language":"C#","funding_links":["https://github.com/sponsors/soenneker","https://thanks.dev/soenneker"],"categories":["GamePlay"],"sub_categories":["Skill"],"readme":"[![](https://img.shields.io/nuget/v/Soenneker.Utils.BackgroundQueue.svg?style=for-the-badge)](https://www.nuget.org/packages/Soenneker.Utils.BackgroundQueue/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/soenneker.utils.backgroundqueue/publish-package.yml?style=for-the-badge)](https://github.com/soenneker/soenneker.utils.backgroundqueue/actions/workflows/publish-package.yml)\n[![](https://img.shields.io/nuget/dt/Soenneker.Utils.BackgroundQueue.svg?style=for-the-badge)](https://www.nuget.org/packages/Soenneker.Utils.BackgroundQueue/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/soenneker.utils.backgroundqueue/codeql.yml?label=CodeQL\u0026style=for-the-badge)](https://github.com/soenneker/soenneker.utils.backgroundqueue/actions/workflows/codeql.yml)\n\n# ![](https://user-images.githubusercontent.com/4441470/224455560-91ed3ee7-f510-4041-a8d2-3fc093025112.png) Soenneker.Utils.BackgroundQueue\n\n### A high-performance background Task / ValueTask queue\n\n---\n\n## Overview\n\n`BackgroundQueue` provides a fast, controlled way to execute background work in .NET applications.\nIt prevents overload by queueing and processing work asynchronously with configurable limits and built-in tracking.\n\n---\n\n## Features\n\n* Supports both `Task` and `ValueTask`\n* Configurable queue size\n* Tracks running and pending work\n* Simple DI registration\n* Hosted service for automatic background processing\n\n---\n\n## Installation\n\n```sh\ndotnet add package Soenneker.Utils.BackgroundQueue\n```\n\nRegister the queue:\n\n```csharp\nvoid ConfigureServices(IServiceCollection services)\n{\n    services.AddBackgroundQueueAsSingleton();\n}\n```\n\n---\n\n## Starting \u0026 Stopping\n\n### Start\n\n```csharp\nawait serviceProvider.WarmupAndStartBackgroundQueue(cancellationToken);\n```\n\nSynchronous start:\n\n```csharp\nserviceProvider.WarmupAndStartBackgroundQueueSync(cancellationToken);\n```\n\n### Stop\n\n```csharp\nawait serviceProvider.StopBackgroundQueue(cancellationToken);\n```\n\nSynchronous stop:\n\n```csharp\nserviceProvider.StopBackgroundQueueSync(cancellationToken);\n```\n\n---\n\n## Configuration\n\n```json\n{\n  \"Background\": {\n    \"QueueLength\": 5000,\n    \"LockCounts\": false,\n    \"Log\": false\n  }\n}\n```\n\n* `QueueLength` � Maximum number of queued items\n* `LockCounts` � Enables thread-safe tracking of running work\n* `Log` � Enables debug logging\n\n---\n\n## Using the Queue\n\nInject `IBackgroundQueue`:\n\n```csharp\nIBackgroundQueue _queue;\n\nvoid MyClass(IBackgroundQueue queue)\n{\n    _queue = queue;\n}\n```\n\n### Queueing a `ValueTask`\n\n```csharp\nawait _queue.QueueValueTask(_ =\u003e someValueTask(), cancellationToken);\n```\n\n### Queueing a `Task`\n\n```csharp\nawait _queue.QueueTask(_ =\u003e someTask(), cancellationToken);\n```\n\n---\n\n## ?? Performance Tip: Prefer Stateful Queueing\n\nAvoid capturing variables in lambdas when queueing work. Captured lambdas allocate and can impact performance under load.\n\n### ? Avoid (captures state)\n\n```csharp\nawait _queue.QueueTask(ct =\u003e DoWorkAsync(id, ct));\n```\n\nIf `id` is a local variable, this creates a closure.\n\n---\n\n## ? Recommended: Pass State Explicitly\n\nUse the stateful overloads with `static` lambdas.\n\n### ValueTask\n\n```csharp\nawait _queue.QueueValueTask(\n    myService,\n    static (svc, ct) =\u003e svc.ProcessAsync(ct),\n    ct);\n```\n\n### Task\n\n```csharp\nawait _queue.QueueTask(\n    (logger, id),\n    static (s, ct) =\u003e s.logger.RunAsync(s.id, ct),\n    ct);\n```\n\n**Why this is better:**\n\n* No closure allocations\n* Lower GC pressure\n* Best performance for high-throughput queues\n\nThe non-stateful overloads remain available for convenience, but **stateful queueing is recommended** for hot paths.\n\n---\n\n## Waiting for the Queue to Empty\n\n```csharp\nawait queue.WaitUntilEmpty(cancellationToken);\n```\n\n---\n\n## Task Tracking\n\nCheck if work is still processing:\n\n```csharp\nbool isProcessing = await queueInformationUtil.IsProcessing(cancellationToken);\n```\n\nGet current counts:\n\n```csharp\nvar (taskCount, valueTaskCount) =\n    await queueInformationUtil.GetCountsOfProcessing(cancellationToken);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fsoenneker.utils.backgroundqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoenneker%2Fsoenneker.utils.backgroundqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fsoenneker.utils.backgroundqueue/lists"}