Creates and controls a thread, and fills in a gap in the .NET framework by propagating thread-local variables from parent to child threads, and by providing a ThreadStarting event.
More...
Creates and controls a thread, and fills in a gap in the .NET framework by propagating thread-local variables from parent to child threads, and by providing a ThreadStarting event.
This class is a decorator for the Thread class and thus a drop-in replacement, except that only the most common methods and properties (both static and non-static) are provided.
.NET itself has no support whatsoever from inheriting thread-local variables. Not only are thread locals not inherited from the parent thread, .NET fires no event when a thread starts and a child thread cannot get the thread ID of the thread that created it.
ThreadEx helps work around this problem by automatically propagating ThreadLocalVariable{T} values, and providing the ThreadStarting event, which blocks the parent thread but is called in the child thread. This only works if you use ThreadEx to start the child thread; when using other mechanisms such as System.Threading.Tasks.Task, it is possible to copy thread- local variables from the parent thread using code like this:
int parentThreadId = Thread.CurrentThread.ManagedThreadId;
var task = System.Threading.Tasks.Task.Factory.StartNew(() => {
using (ThreadEx.PropagateVariables(parentThreadId))
DoSomethingOnChildThread();
});
task.Wait();
Be careful, however: you should guarantee that, while you copy the variables, the parent thread is blocked, or that the parent thread will not modify any of them (which may be difficult since variables might exist that you are unaware of, that you do not control).
TLV (thread-local variable) inheritance is needed to use the Ambient Service Pattern
|
| ThreadEx (ParameterizedThreadStart start) |
|
| ThreadEx (ThreadStart start) |
|
| ThreadEx (ParameterizedThreadStart start, int maxStackSize) |
|
| ThreadEx (ThreadStart start, int maxStackSize) |
|
void | Start () |
| Causes the operating system to change the state of the current instance to System.Threading.ThreadState.Running. More...
|
|
virtual void | Start (object parameter) |
| Causes the operating system to change the state of the current instance to System.Threading.ThreadState.Running. Start() does not return until the ThreadStarted event is handled. More...
|
|
void | Abort (object stateInfo) |
| Raises a System.Threading.ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Calling this method usually terminates the thread. More...
|
|
void | Abort () |
|
override int | GetHashCode () |
| Returns a hash code for the current thread. More...
|
|
void | Join () |
| Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping. More...
|
|
bool | Join (int milliseconds) |
| Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping. More...
|
|
bool | Join (TimeSpan timeout) |
|
static ThreadDestructor Loyc.Threading.ThreadEx.PropagateVariables |
( |
int |
parentThreadId | ) |
|
|
inlinestatic |
Manually initializes ThreadLocalVariable{T} objects in a thread that may not have been started via ThreadEx, propagating values from the parent thread. Returns an object for uninitializing the thread.
- Parameters
-
parentThreadId | Id of parent thread. The .NET framework does not make this information available so you must somehow pass this value manually from the parent thread to the child thread. |
- Returns
- An object to be disposed at the end of the thread. This method can be called in a using statement so that this happens automatically:
using(ThreadEx.PropagateVariables(parentThreadId)) { ... }
. It is important to dispose the returned object so that thread-local values can be released to prevent a memory leak.
It is safe to call this method if the thread has already been initialized. In that case, the thread will not be initialized a second time, and the returned value will do nothing when it is disposed.
Be careful with this method: you should guarantee that, while you copy the variables, the parent thread is blocked, or that the parent thread will not modify any of them during the copying process (which may be difficult since variables might exist that you are unaware of, that you do not control).