Class ThreadingHelper
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
threadThreadThe thread to configure.
isBackgroundboolTrue 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
timerTimerThe 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
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
timerTimerThe timer to configure.
startTimeSpanThe start delay before the timer begins execution.
intervalTimeSpanThe interval between timer executions.
Returns
- Timer
The configured timer.
Launch(Thread)
Starts the specified thread.
public static Thread Launch(this Thread thread)
Parameters
threadThreadThe 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
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
threadThreadThe thread whose priority is to be set.
priorityThreadPriorityThe 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
rwReaderWriterLockSlimThe ReaderWriterLockSlim instance.
handlerActionThe 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
rwLockReaderWriterLockSlimThe 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
timeOutTimeSpanThe TimeSpan for which to sleep.
Thread(Action)
Creates a Thread that executes the specified action.
public static Thread Thread(this Action handler)
Parameters
handlerActionThe 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
handlerActionThe 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
handlerAction<T>The action to be executed by the thread.
argTThe argument passed to the action.
Returns
- Thread
A new Thread instance.
Type Parameters
TThe 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
handlerAction<T1, T2>The action to be executed by the thread.
arg1T1The first argument passed to the action.
arg2T2The second argument passed to the action.
Returns
- Thread
A new Thread instance.
Type Parameters
T1The type of the first argument.
T2The 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
handlerAction<T1, T2, T3>The action to be executed by the thread.
arg1T1The first argument passed to the action.
arg2T2The second argument passed to the action.
arg3T3The third argument passed to the action.
Returns
- Thread
A new Thread instance.
Type Parameters
T1The type of the first argument.
T2The type of the second argument.
T3The 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
handlerAction<T1, T2, T3, T4>The action to be executed by the thread.
arg1T1The first argument passed to the action.
arg2T2The second argument passed to the action.
arg3T3The third argument passed to the action.
arg4T4The fourth argument passed to the action.
Returns
- Thread
A new Thread instance.
Type Parameters
T1The type of the first argument.
T2The type of the second argument.
T3The type of the third argument.
T4The type of the fourth argument.
Timer(Action)
Creates a Timer that executes the specified action.
public static Timer Timer(this Action handler)
Parameters
handlerActionThe 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
handlerActionThe 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
handlerAction<T>The action to be executed by the timer.
argTThe argument passed to the action.
Returns
- Timer
A new Timer instance.
Type Parameters
TThe 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
handlerAction<T1, T2>The action to be executed by the timer.
arg1T1The first argument passed to the action.
arg2T2The second argument passed to the action.
Returns
- Timer
A new Timer instance.
Type Parameters
T1The type of the first argument.
T2The 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
handlerAction<T1, T2, T3>The action to be executed by the timer.
arg1T1The first argument passed to the action.
arg2T2The second argument passed to the action.
arg3T3The third argument passed to the action.
Returns
- Timer
A new Timer instance.
Type Parameters
T1The type of the first argument.
T2The type of the second argument.
T3The 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
handlerAction<T1, T2, T3, T4>The action to be executed by the timer.
arg1T1The first argument passed to the action.
arg2T2The second argument passed to the action.
arg3T3The third argument passed to the action.
arg4T4The fourth argument passed to the action.
Returns
- Timer
A new Timer instance.
Type Parameters
T1The type of the first argument.
T2The type of the second argument.
T3The type of the third argument.
T4The 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
namestringThe name of the Mutex.
mutexMutexWhen 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
rwReaderWriterLockSlimThe ReaderWriterLockSlim instance.
handlerActionThe action to execute within the read lock.
timeOutintThe 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
rwReaderWriterLockSlimThe ReaderWriterLockSlim instance.
handlerActionThe action to execute within the upgradeable read lock.
timeOutintThe 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
rwReaderWriterLockSlimThe ReaderWriterLockSlim instance.
handlerActionThe action to execute within the lock.
timeOutintThe 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
rwReaderWriterLockSlimThe ReaderWriterLockSlim instance.
handlerActionThe 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
cultureCultureInfoThe 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
rwReaderWriterLockSlimThe ReaderWriterLockSlim instance.
handlerActionThe 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
rwLockReaderWriterLockSlimThe ReaderWriterLockSlim instance.
Returns
- IDisposable
An IDisposable that releases the write lock.