Table of Contents

Class ThreadingHelper

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

Provides helper methods for thread and timer operations.

public static class ThreadingHelper
Inheritance
ThreadingHelper
Inherited Members

Methods

Background(Thread, bool)

Sets whether the specified thread is a background thread.

public static Thread Background(this Thread thread, bool isBackground)

Parameters

thread Thread

The thread to configure.

isBackground bool

True to set the thread as a background thread; otherwise, false.

Returns

Thread

The configured thread.

Interval(Timer)

Gets the interval associated with the specified timer.

public static TimeSpan Interval(this Timer timer)

Parameters

timer Timer

The timer whose interval is retrieved.

Returns

TimeSpan

The TimeSpan interval of the timer.

Interval(Timer, TimeSpan)

Sets the start and execution interval for the specified timer.

public static Timer Interval(this Timer timer, TimeSpan interval)

Parameters

timer Timer

The timer to configure.

interval TimeSpan

The interval between timer executions.

Returns

Timer

The configured timer.

Interval(Timer, TimeSpan, TimeSpan)

Sets the start delay and execution interval for the specified timer.

public static Timer Interval(this Timer timer, TimeSpan start, TimeSpan interval)

Parameters

timer Timer

The timer to configure.

start TimeSpan

The start delay before the timer begins execution.

interval TimeSpan

The interval between timer executions.

Returns

Timer

The configured timer.

Launch(Thread)

Starts the specified thread.

public static Thread Launch(this Thread thread)

Parameters

thread Thread

The thread to start.

Returns

Thread

The started thread.

Name(Thread, string)

Sets the name of the specified thread.

public static Thread Name(this Thread thread, string name)

Parameters

thread Thread

The thread to name.

name string

The name to set.

Returns

Thread

The thread with the assigned name.

Priority(Thread, ThreadPriority)

Sets the priority of the specified thread.

public static Thread Priority(this Thread thread, ThreadPriority priority)

Parameters

thread Thread

The thread whose priority is to be set.

priority ThreadPriority

The thread priority to set.

Returns

Thread

The thread with the updated priority.

Read(ReaderWriterLockSlim, Action)

Executes the specified action within a read lock.

public static void Read(this ReaderWriterLockSlim rw, Action handler)

Parameters

rw ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

handler Action

The action to execute within the read lock.

ReaderLock(ReaderWriterLockSlim)

Acquires a read lock and returns an IDisposable that releases the lock when disposed.

public static IDisposable ReaderLock(this ReaderWriterLockSlim rwLock)

Parameters

rwLock ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

Returns

IDisposable

An IDisposable that releases the read lock.

Sleep(TimeSpan)

Suspends the current thread for the specified time interval.

public static void Sleep(this TimeSpan timeOut)

Parameters

timeOut TimeSpan

The TimeSpan for which to sleep.

Thread(Action)

Creates a Thread that executes the specified action.

public static Thread Thread(this Action handler)

Parameters

handler Action

The action to be executed by the thread.

Returns

Thread

A new Thread instance.

ThreadInvariant(Action)

Creates a Thread that executes the specified action with invariant culture.

public static Thread ThreadInvariant(this Action handler)

Parameters

handler Action

The action to be executed by the thread.

Returns

Thread

A new Thread instance.

Thread<T>(Action<T>, T)

Creates a Thread that executes the specified action with one argument.

public static Thread Thread<T>(this Action<T> handler, T arg)

Parameters

handler Action<T>

The action to be executed by the thread.

arg T

The argument passed to the action.

Returns

Thread

A new Thread instance.

Type Parameters

T

The type of the argument.

Thread<T1, T2>(Action<T1, T2>, T1, T2)

Creates a Thread that executes the specified action with two arguments.

public static Thread Thread<T1, T2>(this Action<T1, T2> handler, T1 arg1, T2 arg2)

Parameters

handler Action<T1, T2>

The action to be executed by the thread.

arg1 T1

The first argument passed to the action.

arg2 T2

The second argument passed to the action.

Returns

Thread

A new Thread instance.

Type Parameters

T1

The type of the first argument.

T2

The type of the second argument.

Thread<T1, T2, T3>(Action<T1, T2, T3>, T1, T2, T3)

Creates a Thread that executes the specified action with three arguments.

public static Thread Thread<T1, T2, T3>(this Action<T1, T2, T3> handler, T1 arg1, T2 arg2, T3 arg3)

Parameters

handler Action<T1, T2, T3>

The action to be executed by the thread.

arg1 T1

The first argument passed to the action.

arg2 T2

The second argument passed to the action.

arg3 T3

The third argument passed to the action.

Returns

Thread

A new Thread instance.

Type Parameters

T1

The type of the first argument.

T2

The type of the second argument.

T3

The type of the third argument.

Thread<T1, T2, T3, T4>(Action<T1, T2, T3, T4>, T1, T2, T3, T4)

Creates a Thread that executes the specified action with four arguments.

public static Thread Thread<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> handler, T1 arg1, T2 arg2, T3 arg3, T4 arg4)

Parameters

handler Action<T1, T2, T3, T4>

The action to be executed by the thread.

arg1 T1

The first argument passed to the action.

arg2 T2

The second argument passed to the action.

arg3 T3

The third argument passed to the action.

arg4 T4

The fourth argument passed to the action.

Returns

Thread

A new Thread instance.

Type Parameters

T1

The type of the first argument.

T2

The type of the second argument.

T3

The type of the third argument.

T4

The type of the fourth argument.

Timer(Action)

Creates a Timer that executes the specified action.

public static Timer Timer(this Action handler)

Parameters

handler Action

The action to be executed by the timer.

Returns

Timer

A new Timer instance.

TimerInvariant(Action)

Creates a Timer that executes the specified action with invariant culture.

public static Timer TimerInvariant(this Action handler)

Parameters

handler Action

The action to be executed by the timer.

Returns

Timer

A new Timer instance.

Timer<T>(Action<T>, T)

Creates a Timer that executes the specified action with one argument.

public static Timer Timer<T>(this Action<T> handler, T arg)

Parameters

handler Action<T>

The action to be executed by the timer.

arg T

The argument passed to the action.

Returns

Timer

A new Timer instance.

Type Parameters

T

The type of the argument.

Timer<T1, T2>(Action<T1, T2>, T1, T2)

Creates a Timer that executes the specified action with two arguments.

public static Timer Timer<T1, T2>(this Action<T1, T2> handler, T1 arg1, T2 arg2)

Parameters

handler Action<T1, T2>

The action to be executed by the timer.

arg1 T1

The first argument passed to the action.

arg2 T2

The second argument passed to the action.

Returns

Timer

A new Timer instance.

Type Parameters

T1

The type of the first argument.

T2

The type of the second argument.

Timer<T1, T2, T3>(Action<T1, T2, T3>, T1, T2, T3)

Creates a Timer that executes the specified action with three arguments.

public static Timer Timer<T1, T2, T3>(this Action<T1, T2, T3> handler, T1 arg1, T2 arg2, T3 arg3)

Parameters

handler Action<T1, T2, T3>

The action to be executed by the timer.

arg1 T1

The first argument passed to the action.

arg2 T2

The second argument passed to the action.

arg3 T3

The third argument passed to the action.

Returns

Timer

A new Timer instance.

Type Parameters

T1

The type of the first argument.

T2

The type of the second argument.

T3

The type of the third argument.

Timer<T1, T2, T3, T4>(Action<T1, T2, T3, T4>, T1, T2, T3, T4)

Creates a Timer that executes the specified action with four arguments.

public static Timer Timer<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> handler, T1 arg1, T2 arg2, T3 arg3, T4 arg4)

Parameters

handler Action<T1, T2, T3, T4>

The action to be executed by the timer.

arg1 T1

The first argument passed to the action.

arg2 T2

The second argument passed to the action.

arg3 T3

The third argument passed to the action.

arg4 T4

The fourth argument passed to the action.

Returns

Timer

A new Timer instance.

Type Parameters

T1

The type of the first argument.

T2

The type of the second argument.

T3

The type of the third argument.

T4

The type of the fourth argument.

TryGetUniqueMutex(string, out Mutex)

Tries to get a unique Mutex with the specified name.

public static bool TryGetUniqueMutex(string name, out Mutex mutex)

Parameters

name string

The name of the Mutex.

mutex Mutex

When this method returns, contains the Mutex if successful.

Returns

bool

True if the Mutex is unique and acquired; otherwise, false.

TryRead(ReaderWriterLockSlim, Action, int)

Attempts to execute the specified action within a read lock.

public static bool TryRead(this ReaderWriterLockSlim rw, Action handler, int timeOut = 0)

Parameters

rw ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

handler Action

The action to execute within the read lock.

timeOut int

The timeout in milliseconds to wait for acquiring the lock.

Returns

bool

True if the lock was acquired and the action executed; otherwise, false.

TryUpgrade(ReaderWriterLockSlim, Action, int)

Attempts to execute the specified action within an upgradeable read lock.

public static bool TryUpgrade(this ReaderWriterLockSlim rw, Action handler, int timeOut = 0)

Parameters

rw ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

handler Action

The action to execute within the upgradeable read lock.

timeOut int

The timeout in milliseconds to wait for acquiring the lock.

Returns

bool

True if the lock was acquired and the action executed; otherwise, false.

TryWrite(ReaderWriterLockSlim, Action, int)

Attempts to execute the specified action within a write lock.

public static bool TryWrite(this ReaderWriterLockSlim rw, Action handler, int timeOut = 0)

Parameters

rw ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

handler Action

The action to execute within the lock.

timeOut int

The timeout in milliseconds to wait for acquiring the lock.

Returns

bool

True if the lock was acquired and the action executed; otherwise, false.

Upgrade(ReaderWriterLockSlim, Action)

Executes the specified action within an upgradeable read lock.

public static void Upgrade(this ReaderWriterLockSlim rw, Action handler)

Parameters

rw ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

handler Action

The action to execute within the upgradeable read lock.

WithCulture(CultureInfo)

Temporarily sets the current thread's culture to the specified culture.

public static IDisposable WithCulture(CultureInfo culture)

Parameters

culture CultureInfo

The CultureInfo to be set for the current thread.

Returns

IDisposable

An IDisposable that, when disposed, restores the original culture.

WithInvariantCulture()

Temporarily sets the current thread's culture to the invariant culture.

public static IDisposable WithInvariantCulture()

Returns

IDisposable

An IDisposable that, when disposed, restores the original culture.

Write(ReaderWriterLockSlim, Action)

Executes the specified action within a write lock.

public static void Write(this ReaderWriterLockSlim rw, Action handler)

Parameters

rw ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

handler Action

The action to execute within the lock.

WriterLock(ReaderWriterLockSlim)

Acquires a write lock and returns an IDisposable that releases the lock when disposed.

public static IDisposable WriterLock(this ReaderWriterLockSlim rwLock)

Parameters

rwLock ReaderWriterLockSlim

The ReaderWriterLockSlim instance.

Returns

IDisposable

An IDisposable that releases the write lock.