C# Thread implementation with .NET Framework
Properties
Name | Description |
---|---|
IsAlive | Checks if session is alive |
IsBackground | Checks if thread runs as a background thread |
IsThreadPoolThread | Checks if a thread in the thread pool |
ManagedThreadId | Gets a number for the current thread |
Name | Manages a name associated with the thread |
Priority | Manages the priority of the thread |
ThreadState | Gets the ThreadState value |
Methods
Name | Description |
---|---|
Abort | Raises a ThreadAbortException should be aborted |
Interrupt | Raises a ThreadInterruptedException when a thread is in a blocked position |
Join | Blocks the calling thread |
Start | Sets a thread to be scheduled for execution |
Static Properties
Name | Description |
---|---|
CurrentContext | Obtains the current ThreadContext |
CurrentPrincipal | Obtains and sets the user |
CurrentThread | Obtains the current running thread |
Static Methods
Name | Description |
---|---|
BeginCriticalRegion | Notifies if thread executed cannot be aborted safely |
EndCriticalRegion | Notifies the host that he end of a critical region reached |
GetDomain | Gets the AppDomain |
GetDomainID | Gets a unique identifier for AppDomain |
ResetAbort | Cancels an Abort request |
Sleep | Blocks the current thread |
SpinWait | Blocks the current thread for a certain number of iterations |
VolatileRead | Gets the latest version of a field value |
VolatileWrite | Writes a field value immediately |
In addition to Thread we require to use ThreadState enumeration
Name | Description |
---|---|
Aborted | Thread aborted |
AbortRequested | Thread requested to abort |
Background | Thread is running as a backgroun |
Running | Thread started |
Stopped | Thread stopped |
StopRequested | Thread requested to stop |
Suspended | Thread suspended |
SuspendedRequested | Thread requested to suspend |
Unstarted | Thread created |
WaitSleepJoin | Thread blocked |
We start the thread and normally its more than just one thread and here is how we do it.
static void SimpleThreadWork()
{
for (int x = 1; x <= 10; ++x)
{
Console.WriteLine("Thread: {0}", Thread.CurrentThread.ManagedThreadId);
// Sleep for 10 mil. Sec.
Thread.Sleep(10);
}
}
The result of running this code will be quite interesting since multiple threads will be executing in random order. In order to make sure that our main thread work until all other thread it spawned finished we use Thread.Join method. This method makes sure that application does not stop working until all the threads finished. We store thread in array and then loop until we return from the loop and we know all threads are finished.
ThreadStart myOperation = new ThreadStart(SomeWork);
Thread[] theMineThreads = new Thread[5];
for (int x = 0; x < 5; ++x)
{
// Creates
theMineThreads[x] = new Thread(myOperation);
// Starts
theMineThreads[x].Start();
}
// Waits
foreach (Thread t in theMineThreads)
{
t.Join();
}
Thread class provide ThreadPriority enumeration. Default priority is Normal. The disadvantage of setting priority to high or too low can case thread starvation.
Name |
---|
Highest |
AboveNormal |
Normal |
BelowNormal |
Lowest |