Class ThreadLord<T>
Process a collection of tasks as fast as possible using a thread pool.
Implements
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public class ThreadLord<T> : IDisposable
Type Parameters
| Name | Description |
|---|---|
| T | The type of tasks to process. |
Constructors
ThreadLord(Action<T>)
Initializes a new instance of the ThreadLord class with a default maximum thread count.
Declaration
public ThreadLord(Action<T> onWork)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onWork | The callback action for processing tasks. |
ThreadLord(Action<T>, int)
Initializes a new instance of the ThreadLord class with a specified maximum thread count.
Declaration
public ThreadLord(Action<T> onWork, int maxThreads)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<T> | onWork | The callback action for processing tasks. |
| int | maxThreads | The maximum number of worker threads. |
Properties
CurrentlyProcessing
Gets the number of worker threads currently processing tasks.
Declaration
public long CurrentlyProcessing { get; }
Property Value
| Type | Description |
|---|---|
| long |
Enqueued
Gets the number of tasks currently enqueued.
Declaration
public long Enqueued { get; }
Property Value
| Type | Description |
|---|---|
| long |
Failed
Gets the total number of failed tasks.
Declaration
public long Failed { get; }
Property Value
| Type | Description |
|---|---|
| long |
Processed
Gets the total number of processed tasks.
Declaration
public long Processed { get; }
Property Value
| Type | Description |
|---|---|
| long |
ProcessedLastSecond
Gets the count of tasks processed in the last second.
Declaration
public long ProcessedLastSecond { get; }
Property Value
| Type | Description |
|---|---|
| long |
ProcessedPerSecondAverage
Gets the average number of tasks processed per second.
Declaration
public long ProcessedPerSecondAverage { get; }
Property Value
| Type | Description |
|---|---|
| long |
QueueLimit
Gets the queue size limit.
Declaration
public long QueueLimit { get; }
Property Value
| Type | Description |
|---|---|
| long |
State
Gets the current state of the ThreadLord instance.
Declaration
public ThreadLord<T>.LordState State { get; }
Property Value
| Type | Description |
|---|---|
| ThreadLord<T>.LordState |
Workers
Gets the number of worker threads currently active.
Declaration
public long Workers { get; }
Property Value
| Type | Description |
|---|---|
| long |
Methods
Clear()
Clears all tasks from the queue.
Declaration
public void Clear()
Dispose()
Disposes of the ThreadLord instance and stops processing.
Declaration
public void Dispose()
Enqueue(T)
Enqueues a task to be processed.
Declaration
public void Enqueue(T data)
Parameters
| Type | Name | Description |
|---|---|---|
| T | data | The task data to enqueue. |
LimitQueue(int)
Sets the maximum size of the task queue.
Declaration
public ThreadLord<T> LimitQueue(int limit)
Parameters
| Type | Name | Description |
|---|---|---|
| int | limit | The maximum number of tasks to be queued. |
Returns
| Type | Description |
|---|---|
| ThreadLord<T> | The ThreadLord instance with the queue size limit set. |
OnError(Action<Exception>)
On error callback.
Declaration
public ThreadLord<T> OnError(Action<Exception> onError)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<Exception> | onError |
Returns
| Type | Description |
|---|---|
| ThreadLord<T> |
Start()
Starts processing tasks using worker threads.
Declaration
public ThreadLord<T> Start()
Returns
| Type | Description |
|---|---|
| ThreadLord<T> | The ThreadLord instance in the Running state. |
Stop()
Stops processing tasks.
Declaration
public ThreadLord<T> Stop()
Returns
| Type | Description |
|---|---|
| ThreadLord<T> | The ThreadLord instance in the Stopping state. |
WaitAll(Action)
Waits for all tasks to be processed.
Declaration
public ThreadLord<T> WaitAll(Action whileWaiting = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | whileWaiting | An optional action to perform while waiting for tasks to complete. |
Returns
| Type | Description |
|---|---|
| ThreadLord<T> | The ThreadLord instance after all tasks are processed. |
WaitAllAndStop(Action)
Waits for all tasks to be processed and then stops the processing.
Declaration
public ThreadLord<T> WaitAllAndStop(Action whileWaiting = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | whileWaiting | An optional action to perform while waiting for tasks to complete. |
Returns
| Type | Description |
|---|---|
| ThreadLord<T> | The ThreadLord instance after all tasks are processed and processing is stopped. |