Abandon.ThreadPoolHelper
1.1.1
dotnet add package Abandon.ThreadPoolHelper --version 1.1.1
NuGet\Install-Package Abandon.ThreadPoolHelper -Version 1.1.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Abandon.ThreadPoolHelper" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Abandon.ThreadPoolHelper" Version="1.1.1" />
<PackageReference Include="Abandon.ThreadPoolHelper" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Abandon.ThreadPoolHelper --version 1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Abandon.ThreadPoolHelper, 1.1.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Abandon.ThreadPoolHelper@1.1.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Abandon.ThreadPoolHelper&version=1.1.1
#tool nuget:?package=Abandon.ThreadPoolHelper&version=1.1.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
🧵 ThreadHelper
高可用线程任务队列调度器(生产级)
提供最大并发控制、队列长度限制、任务超时、统一返回结果等特性。
✨ 特性
- 最大并发控制:通过信号量控制同时执行的任务数,可动态调整。
- 队列长度限制:防止任务队列无限增长。
- 任务超时:支持每个任务设置超时时间。
- 外部取消:支持外部
CancellationToken,任务可被取消。 - 统一 TaskResult 返回:封装任务执行结果,包括成功、超时、取消、异常。
⚡ 安装
Install-Package Abandon.ThreadPoolHelper
适用于:
- .NET Standard 2.0+
- .NET Framework 4.6.1+
- .NET Core / .NET 5/6/7/8
🚀 使用示例
using System;
using System.Threading;
using System.Threading.Tasks;
using Abandon.ThreadPoolHelper;
class Program
{
static async Task Main()
{
var threadHelper = new ThreadHelper(10, 100, (msg, ex) =>
{
Console.WriteLine($"{msg}: {ex.Message}");
});
var resultTask = threadHelper.EnqueueTask(async ct =>
{
await Task.Delay(1000, ct);
}, TimeSpan.FromSeconds(5));
var result = await resultTask;
Console.WriteLine($"Task Status: {result.Status}");
await threadHelper.FlushAsync();
threadHelper.Dispose();
}
}
🛠 API
构造函数
ThreadHelper(int maxConcurrency = 500, int maxQueueLength = 10000, Action<string, Exception> onError = null)
| 参数 | 类型 | 描述 |
|---|---|---|
| maxConcurrency | int | 最大并发任务数 |
| maxQueueLength | int | 队列最大长度 |
| onError | Action<string, Exception> | 异常回调,可选 |
方法
| 方法 | 描述 |
|---|---|
Task<TaskResult> EnqueueTask(Func<CancellationToken, Task> taskFunc, TimeSpan timeout = default, CancellationToken externalCancellation = default) |
入队任务并返回 TaskResult |
void UpdateMaxConcurrency(int newMax) |
动态调整最大并发数 |
int GetRunningCount() |
获取当前正在运行的任务数 |
int GetQueueLength() |
获取当前队列长度 |
Task FlushAsync() |
等待队列清空并所有任务完成 |
void Dispose() |
释放信号量和内部资源 |
TaskResult
| 状态 | 描述 |
|---|---|
| Success | 任务成功完成 |
| Timeout | 任务超时 |
| Canceled | 任务被取消 |
| Faulted | 任务发生异常 |
⚠ 注意事项
- 调整最大并发数时,不会中断正在执行的任务。
- 队列已满时,入队任务会立即返回
Faulted。 - 使用完
ThreadHelper后请调用Dispose,释放信号量和内部资源。
📬 联系
作者: Cooper
邮箱: W47887406@live.com
Made with ❤️ by ThreadHelper
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.