Table of Contents

Class ThreadWorker<T>

Namespace
CMS.Base
Assembly
CMS.Base.dll

Worker processing actions in single (one per application and generic variant), ever-running asynchronous thread.

public abstract class ThreadWorker<T> where T : ThreadWorker<T>, new()

Type Parameters

T
Inheritance
object
ThreadWorker<T>
Derived
Extension Methods

Fields

LOG_MAX_LENGTH

Maximum length of the worker log to prevent memory leaks

protected const int LOG_MAX_LENGTH = 102400

Field Value

int

SyncRoot

Lock this object when doing actions that might change the result of condition run while stopping execution

protected readonly object SyncRoot

Field Value

object

Properties

Current

Current thread worker object

public static T Current { get; }

Property Value

T

DefaultInterval

Gets the default interval in milliseconds for the worker.

protected abstract int DefaultInterval { get; }

Property Value

int

MaintenanceInterval

Gets the maintenance interval in milliseconds for the worker. When 0 (default), the maintenance is not performed.

protected virtual int MaintenanceInterval { get; }

Property Value

int

PollThread

Gets or sets thread which is periodically checking for new tasks.

protected Thread PollThread { get; set; }

Property Value

Thread

ProcessingPaused

Indicates whether worker routine is temporarily paused.

public bool ProcessingPaused { get; set; }

Property Value

bool

Remarks

This property supports the framework infrastructure and is not intended to be used directly from your code.

StopRequestToken

The System.Threading.CancellationToken to be checked if the thread should stop execution.

protected CancellationToken StopRequestToken { get; }

Property Value

CancellationToken

UseLogContext

If true, the thread uses a log context for its operations

[Obsolete("Member is deprecated and will be removed in next version.")]
[ObsoleteSince(28, 0)]
protected virtual bool UseLogContext { get; }

Property Value

bool

Methods

CreateThreadSettings()

Creates ThreadSettings object and enables the IsBackground and UseEmptyContext properties.

protected virtual ThreadSettings CreateThreadSettings()

Returns

ThreadSettings

Dispose()

Disposes instance of thread worker.

public void Dispose()

Dispose(bool)

Disposes instance of thread worker.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

Indicates to dispose managed resources.

DoMaintenance()

Runs the maintenance routine for the worker

protected virtual void DoMaintenance()

Remarks

If exception from override arises, the thread will not end, but exception will not be logged. Custom implementation of exception handling is required.

EnsureRunningThread()

Ensures a running thread for this processor

public void EnsureRunningThread()

Finish()

Finishes the worker process.

protected abstract void Finish()

Remarks

Implement this method to specify what the worker must do in order to not lose its internal data when being finished.

Execution or completeness is not guaranteed. Do not use this method for time-consuming actions. Code execution can be forcibly interrupted after a short period of time.

The Finish() method is called in following scenarios:

Initialize()

Initializes the worker. Runs in the worker thread before the thread processes the first iteration.

protected virtual void Initialize()

IsThreadRunning()

Returns true if the worker thread is currently running

public bool IsThreadRunning()

Returns

bool

Remarks

This method supports the framework infrastructure and is not intended to be used directly from your code.

Process()

Method processing actions.

protected abstract void Process()

Remarks

If exception from override arises, the thread will not end, but exception will not be logged. Custom implementation of exception handling is required.

RunProcess()

Runs the internal process of the worker

protected void RunProcess()

StopExecution(Func<bool>)

Stops the worker after finishing its job.

protected void StopExecution(Func<bool> condition = null)

Parameters

condition Func<bool>

Enables you to cancel stopping the worker by returning false from the function.

Remarks

You should use parameter condition to make sure that it is safe to stop the worker and no data are lost.