A comprehensive and efficient low-contention thread pool for easily managing both sync and async workloads. It provides granular work control, flexible concurrency, and robust error handling.
Enables efficient thread pool management with callback implementation, granular control, customizable concurrency, and support for diverse task submissions.
PowerPool powerPool = new PowerPool(new ThreadPoolOption() { /* Some options */ });
powerPool.QueueWorkItem(() =>
{
// DO SOMETHING
});
With callback
PowerPool powerPool = new PowerPool(new ThreadPoolOption() { /* Some options */ });
powerPool.QueueWorkItem(() =>
{
// DO SOMETHING
return result;
}, (res) =>
{
// this callback of thread
// running result: res.Result
});
With option
PowerPool powerPool = new PowerPool(new ThreadPoolOption() { /* Some options */ });
powerPool.QueueWorkItem(() =>
{
// DO SOMETHING
return result;
}, new ThreadOption()
{
// Some options
});
API Summary
PowerPool
name
summary
result
QueueWorkItem<...>(...)
Queues a method for execution. The method executes when a thread pool thread becomes available.
work id
Wait()
Blocks the calling thread until all of the threads terminates.
-
Wait(string id)
Blocks the calling thread until the thread terminates.
Return false if the thread isn't running
WaitAsync()
Blocks the calling thread until all of the threads terminates.
Task
WaitAsync(string id)
Blocks the calling thread until the thread terminates.
Return false if the thread isn't running
Stop(bool forceStop = false)
Stop all threads. If forceStop is true, Thread.Interrupt() and Thread.Join() will be called.
Return false if no thread running
StopAsync(bool forceStop = false)
Stop all threads. If forceStop is true, Thread.Interrupt() and Thread.Join() will be called.
(Task) Return false if no thread running
Stop(string id, bool forceStop = false)
Stop thread by id. If forceStop is true, Thread.Interrupt() and Thread.Join() will be called.
Return false if the thread isn't running
StopAsync(string id, bool forceStop = false)
Stop thread by id. If forceStop is true, Thread.Interrupt() and Thread.Join() will be called.
(Task) Return false if the thread isn't running
PauseIfRequested()
Call this function inside the thread logic where you want to pause when user call Pause(...)
-
StopIfRequested()
Call this function inside the thread logic where you want to stop when user call Stop(...)
-
CheckIfRequestedStop()
Call this function inside the thread logic where you want to check if requested stop (if user call Stop(...))
-
Pause()
Pause all threads
-
Resume(bool resumeThreadPausedById = false)
Resume all threads
-
Pause(string id)
Pause thread by id
-
Resume(string id)
Resume thread by id
-
Cancel()
Cancel all tasks that have not started running
-
Cancel(string id)
Cancel the task by id if the task has not started running
is succeed
API List
PowerPool
Properties
bool ThreadPoolRunning // Get
int IdleThreadCount; // Get
ThreadPoolOption ThreadPoolOption; // Get, Set
int WaitingWorkCount; // Get
IEnumerable<string> WaitingWorkerList; // Get
int RunningWorkerCount; // Get
IEnumerable<string> RunningWorkList; // Get
// Work id.
string ID; // Get
// Result of the work.
TResult Result; // Get
// Succeed or failed.
Status Status; // Get
// If failed, Exception will be setted here.
Exception Exception; // Get
Status
enum Status { Succeed, Failed }
ThreadPoolOption
Properties
// The maximum number of threads that the thread pool can support.
int MaxThreads; // Get, Set
// The option for destroying threads in the thread pool.
DestroyThreadOption DestroyThreadOption; // Get, Set
// The total maximum amount of time that all threads in the thread pool are permitted to run collectively before they are terminated.
TimeoutOption Timeout; // Get, Set
// The default maximum amount of time a thread in the pool is allowed to run before it is terminated.
TimeoutOption DefaultThreadTimeout; // Get, Set
// The default callback function that is called when a thread finishes execution.
Action<ExecuteResult<object>> DefaultCallback; // Get, Set
DestroyThreadOption
Properties
// The amount of time a thread is kept alive after it finishes execution. If a new task is received within this time, the thread is reused; otherwise, it is destroyed.
int KeepAliveTime; // Get, Set
// The minimum number of threads that the thread pool should maintain at all times.
int MinThreads; // Get, Set
ThreadOption
Properties
// The custom work ID. If set to null, the thread pool will use a Guid as the work ID.
string CustomWorkID; // Get, Set
// The maximum amount of time the thread is allowed to run before it is terminated.
TimeoutOption Timeout; // Get, Set
// The callback function that is called when the thread finishes execution.
Action<ExecuteResult<TResult>> Callback; // Get, Set
// The priority level of the thread. Higher priority threads are executed before lower priority threads.
int Priority; // Get, Set
// A set of threads that this thread depends on. This thread will not start until all dependent threads have completed execution.
HashSet<string> Dependents; // Get, Set
TimeoutOption
Properties
// The maximum amount of time (ms)
int Duration;
// If forceStop is true, Thread.Interrupt() and Thread.Join() will be called.
bool ForceStop;