Show / Hide Table of Contents

Class ThreadManager

Thread handler for asynchronous operations

Inheritance
Object
ThreadManager
Namespace: SuperOffice.Threading
Assembly: SoCore.dll
Syntax
public static class ThreadManager : Object
Remarks

Use to start a new thread in the same SoContext as the current thread.

Fields

_maxSimultaneousThreads

Thread handler for asynchronous operations

Declaration
public static int _maxSimultaneousThreads
Field Value
Type Description
Int32
Remarks

Use to start a new thread in the same SoContext as the current thread.

_threadOverflows

Thread handler for asynchronous operations

Declaration
public static int _threadOverflows
Field Value
Type Description
Int32
Remarks

Use to start a new thread in the same SoContext as the current thread.

_threadOverflowsForced

Thread handler for asynchronous operations

Declaration
public static int _threadOverflowsForced
Field Value
Type Description
Int32
Remarks

Use to start a new thread in the same SoContext as the current thread.

Properties

HasReachedMaxThreads

Gets whether the ThreadManager has started more than MaxThreads threads which has yet to complete.

If true, spawning new threads via SpawnThread<TArgument>(AsyncOperation<TArgument>, TArgument, String, Boolean, Boolean, Boolean) will block.

Declaration
public static bool HasReachedMaxThreads { get; }
Property Value
Type Description
Boolean
Remarks

Use to start a new thread in the same SoContext as the current thread.

IsWorkerThread

Is this thread executed by the ThreadManager.

Declaration
public static bool IsWorkerThread { get; }
Property Value
Type Description
Boolean
Remarks

This can be used by a

MaxThreads

Maximum parallel operations permitted to run simultaneously. (-1 is infinite)

Declaration
public static int MaxThreads { get; }
Property Value
Type Description
Int32
Remarks

Use to start a new thread in the same SoContext as the current thread.

RunningThreadCount

Thread handler for asynchronous operations

Declaration
public static int RunningThreadCount { get; }
Property Value
Type Description
Int32
Remarks

Use to start a new thread in the same SoContext as the current thread.

SingleThreadMode

Are we running in single-thread mode? If true, then all calls to Spawn will execute in-line; and all calls to Wait will return immediately

Declaration
public static bool SingleThreadMode { get; }
Property Value
Type Description
Boolean
Remarks

Use to start a new thread in the same SoContext as the current thread.

Methods

BeginIgnoreRowSentry()

Thread handler for asynchronous operations

Declaration
public static ThreadManager.IgnoreRowSentryModifier BeginIgnoreRowSentry()
Returns
Type Description
ThreadManager.IgnoreRowSentryModifier
Remarks

Use to start a new thread in the same SoContext as the current thread.

BeginIgnoreSentry()

Thread handler for asynchronous operations

Declaration
public static ThreadManager.IgnoreSentryModifier BeginIgnoreSentry()
Returns
Type Description
ThreadManager.IgnoreSentryModifier
Remarks

Use to start a new thread in the same SoContext as the current thread.

BeginPreventParallelism()

Temporary enter a state where all parallel operations started from the current thread is executed syncroniously. Call using a using (ThreadManager.BeginPreventParallelism()){...} constrinction.

Declaration
public static ThreadManager.ForceSingleThreadHelper BeginPreventParallelism()
Returns
Type Description
ThreadManager.ForceSingleThreadHelper

Disposable object.

Remarks

Use to start a new thread in the same SoContext as the current thread.

IgnoreRowSentry()

Thread-static, principal-independent "should row sentry be ignored if possible"

Declaration
public static bool IgnoreRowSentry()
Returns
Type Description
Boolean
Remarks

Use to start a new thread in the same SoContext as the current thread.

IgnoreSentry()

Thread-static, principal-independent "should sentry be ignored"

Declaration
public static bool IgnoreSentry()
Returns
Type Description
Boolean
Remarks

Use to start a new thread in the same SoContext as the current thread.

Invoke(Action[])

Invoke the given actions in parallel, and return when they're all done

Declaration
public static void Invoke(params Action[] actions)
Parameters
Type Name Description
Action[] actions

Array of actions to be performed in parallell

Remarks

No synchronization logic is performed between actions, so they have to either be independent or synchronized on their own.

Early-out logic handles cases of null, 0, or 1 items in the Action array. If single-thread mode is active, actions are called in sequence (in array order).

Fully concurrent execution is NOT guaranteed - throttling, thread count limits, single-thread mode or other factors (such as hardware!) will influence actual execution order and concurrency.

The method returns when all actions have been performed.

RegisterAsyncContextProvider<TThreadState, TThreadCleanupState>(GetThreadState<TThreadState>, SetThreadState<TThreadState, TThreadCleanupState>, CleanupThreadState<TThreadState, TThreadCleanupState>)

Register methods for passing state from calling thread to created threads.

Declaration
public static void RegisterAsyncContextProvider<TThreadState, TThreadCleanupState>(GetThreadState<TThreadState> getThreadState, SetThreadState<TThreadState, TThreadCleanupState> setThreadState, CleanupThreadState<TThreadState, TThreadCleanupState> cleanupThreadState)
Parameters
Type Name Description
GetThreadState<TThreadState> getThreadState

Method called in calling thread initiating new thread to obtain state that can be passed on to the new thread.

SetThreadState<TThreadState, TThreadCleanupState> setThreadState

Apply state on new thread.

CleanupThreadState<TThreadState, TThreadCleanupState> cleanupThreadState

Clean up state applied on new thread, so it can be re-used later.

Type Parameters
Name Description
TThreadState

State passed between calling thread and newly started thread.

TThreadCleanupState

State pased from initilaizing new thread to clean-up code.

Remarks

Use to start a new thread in the same SoContext as the current thread.

SpawnThread<TArgument>(AsyncOperation<TArgument>, TArgument, String, Boolean, Boolean, Boolean)

Spawn a new thread in the same SoContext as the currently executed thread.

Declaration
public static AsyncContext SpawnThread<TArgument>(AsyncOperation<TArgument> asyncOperation, TArgument argument, string threadName = null, bool breakOnError = true, bool disableTimeLogging = false, bool forceNewThreadEvenIfAtMax = false)
Parameters
Type Name Description
AsyncOperation<TArgument> asyncOperation

Operation executed in the new thread.

TArgument argument

Argument passed to the operation executing the thread.

String threadName

Thread name. Default null.

Boolean breakOnError

Shall the atteched debugger break if there is an error executing the thread. Default true.

Boolean disableTimeLogging

Disable logging of times on the inner spawned thread. Default false.

Boolean forceNewThreadEvenIfAtMax
Returns
Type Description
AsyncContext

Information about the async operation.

Type Parameters
Name Description
TArgument
Remarks

If the number of threads currently executing in parallel is less then the currently configured maximum allowed threads for parallel execution, a thread is spawned. If the number of threads exceeds this limitation, the operation will be performed synchronously on the current thread.

Maximum parallel connections are read from the property of the configuration file

Examples

The method implementing the asynchronous operation needs to implement the delegate :

private void MyAsyncOperation(AsyncContext context, object argument)
{
	// Do some stuff...
}

The argument passed to the method is an optional argument selected when spawning off the asynchronous task:

AsyncContext ctx = ThreadManager.SpawnThread(new AsyncOperation(MyAsyncOperation), argument);

SpawnThreadOrExecuteSync<TArgument>(AsyncOperation<TArgument>, TArgument, String)

Spawn a new thread in the same SoContext as the currently executed thread, or if blocked execute code in current thread.

This method is aimed to be used in critical functions where you want parellism and speed when possible, but cannot afford to be blocked by an empty threadpool.

Declaration
public static AsyncContext SpawnThreadOrExecuteSync<TArgument>(AsyncOperation<TArgument> asyncOperation, TArgument argument, string threadName)
Parameters
Type Name Description
AsyncOperation<TArgument> asyncOperation

Operation executed in the new thread.

TArgument argument

Argument passed to the operation executing the thread.

String threadName

Thread name. Default null.

Returns
Type Description
AsyncContext

Information about the async operation.

Type Parameters
Name Description
TArgument
Remarks

If the number of threads currently executing in parallel is less then the currently configured maximum allowed threads for parallel execution, a thread is spawned. If the number of threads exceeds this limitation, the current thread is blocked until the number of threads currently executing in parallel is less then the currently configured maximum allowed threads for parallel execution.

Maximum parallel connections are read from the property of the configuration file

Examples

The method implementing the asynchronous operation needs to implement the delegate :

private void MyAsyncOperation(AsyncContext context, object argument)
{
	// Do some stuff...
}

The argument passed to the method is an optional argument selected when spawning off the asynchronous task:

AsyncContext ctx = ThreadManager.SpawnThreadOrExecuteSync(new AsyncOperation(MyAsyncOperation), argument);

StartNew(Action, Boolean)

Start the given Action in a new thread, and return the corresponding AsynContext so the client can wait for it to complete

Declaration
public static AsyncContext StartNew(Action action, bool forceNewThreadCreation = false)
Parameters
Type Name Description
Action action

Action (method, lambda) to perform

Boolean forceNewThreadCreation
Returns
Type Description
AsyncContext

Async context you can wait for at some later point

Remarks

Use to start a new thread in the same SoContext as the current thread.

StartNew(Action, String, Boolean)

Start the given Action in a new thread, and return the corresponding AsynContext so the client can wait for it to complete

Declaration
public static AsyncContext StartNew(Action action, string threadName, bool forceNewThreadCreation = false)
Parameters
Type Name Description
Action action

Action (method, lambda) to perform

String threadName

Thread name

Boolean forceNewThreadCreation
Returns
Type Description
AsyncContext

Async context you can wait for at some later point

Remarks

Use to start a new thread in the same SoContext as the current thread.

StartNew<T>(T, Action<T>, Boolean)

Start the given Action in a new thread, and return the corresponding AsynContext so the client can wait for it to complete

Declaration
public static AsyncContext StartNew<T>(T argument, Action<T> action, bool forceNewThreadCreation = false)
Parameters
Type Name Description
T argument

Argument passed to the action

Action<T> action

Action (method, lambda) to perform

Boolean forceNewThreadCreation
Returns
Type Description
AsyncContext

Async context you can wait for at some later point

Type Parameters
Name Description
T
Remarks

Use to start a new thread in the same SoContext as the current thread.

StartNew<T>(T, Action<T>, String, Boolean)

Start the given Action in a new thread, and return the corresponding AsynContext so the client can wait for it to complete

Declaration
public static AsyncContext StartNew<T>(T argument, Action<T> action, string threadName, bool forceNewThreadCreation = false)
Parameters
Type Name Description
T argument

Argument passed to the action

Action<T> action

Action (method, lambda) to perform

String threadName

Thread name

Boolean forceNewThreadCreation
Returns
Type Description
AsyncContext

Async context you can wait for at some later point

Type Parameters
Name Description
T
Remarks

Use to start a new thread in the same SoContext as the current thread.

WaitForAllOperationsInDatabaseContextToComplete()

Wait until all asynchronous operations in hte current database context have completed. This includes n-th generation child threads.

Declaration
public static void WaitForAllOperationsInDatabaseContextToComplete()
Remarks

Use to start a new thread in the same SoContext as the current thread.

WaitForAllOperationsToComplete()

Wait until all asynchronous operations have completed. This includes n-th generation child threads.

Declaration
public static void WaitForAllOperationsToComplete()
Remarks

Use to start a new thread in the same SoContext as the current thread.

WaitForAllOperationsToComplete(Int32, Boolean)

Wait until all asynchronous operations in the current database context have completed, or the time limit (ms) runs out. This includes n-th generation child threads

Declaration
public static bool WaitForAllOperationsToComplete(int timeLimit, bool inCurrentDatabaseContextOnly = false)
Parameters
Type Name Description
Int32 timeLimit

max number of milliseconds to wait, 0 means forever

Boolean inCurrentDatabaseContextOnly

Only check for threads in the same database context and ignore the state of other threads.

Returns
Type Description
Boolean

true if all operations finished before the timeout hit

Remarks

Use to start a new thread in the same SoContext as the current thread.

WaitForOperationsToComplete(AsyncContext[])

Use this method to wait for all of the given threads to complete; this overload will wait forever.

Declaration
public static bool WaitForOperationsToComplete(params AsyncContext[] contexts)
Parameters
Type Name Description
AsyncContext[] contexts

Array of SoContexts describing the threads to wait for; null values are allowed and will just be skipped

Returns
Type Description
Boolean

True if all operations had time to complete; false if the timeout triggered

Remarks

Use to start a new thread in the same SoContext as the current thread.

WaitForOperationsToComplete(Int32, AsyncContext[])

Use this method to wait for all of the given threads to complete, with a timeout

Declaration
public static bool WaitForOperationsToComplete(int timeout, params AsyncContext[] contexts)
Parameters
Type Name Description
Int32 timeout

Timeout in milliseconds; use Timeout.Infinite or the other overload to waut forever

AsyncContext[] contexts

Array of SoContexts describing the threads to wait for; null values are allowed and will just be skipped

Returns
Type Description
Boolean

True if all operations had time to complete; false if the timeout triggered

Remarks

Use to start a new thread in the same SoContext as the current thread.

© SuperOffice. All rights reserved.
SuperOffice |  Community |  Release Notes |  Privacy |  Site feedback |  Search Docs |  About Docs |  Contribute |  Back to top