Threads

Added in .NET 1.1
A thread is a unit of scheduling
Threads can only have one task running at a time.
Threads typically represent multiple flows of execution in an application (or process)
If you are creating more than one thread you need to have a 1:1 relationship between the number of threads and the number of cores in the CPU.
Each thread can run in a separate processor on a multi-processor computer.
By default each thread executes independently


System.Threading.Thread myThread; 
myThread = new System.Threading.Thread(delegate() {});
myThread.Start();

Using Threads

Every time you create a thread it uses up more memory.
In fact about 1 MB of memory per thread.
When you are using threads you should always use a Thread Pool to help recycle the threads.


Thread Priority

Highest
AboveNormal
Normal
BelowNorml
Lowest


Thread Class

The Thread class is used for creating and manipulating a thread in Windows.



Application Domains

An App Domain can contain one or more threads.
Threads can cross between different App Domains.
Applications > Application Domains


STA - Single Threaded Apartment


System.Threading.Thread.CurrentThread.TrySetApartmentState

This sets the apartment state of a thread.
(System.Threading.ApartmentState.STA)
Returns true if the apartment state is set
Returns true if the apartment state is not set


Single Threaded Apartment

The STA (Single Threaded Apartment) can have only one thread.
When an object created on a STA thread is called from another thread, this other thread posts windows messages into the message queue of the STA thread.


You can use Queues with multi-threading in .NET but you must use the necessary lock statements.


Lots of windows code requires you to follow STA
such as: clipboard, drag and drop, windows common dialog boxes, UI thread for WPF, windows forms, multiple windows


Threads are initialized as ApartmentState.MTA by default.
The apartment state cannot be changed once the thread has been created.


Communication between apartments is done via marshalling


System.Threading.Thread.CurrentThread 

Thread Safe Blocking Queue.
Interlocked classes


Methods

Abort - this raises a ThreadAbortException. Aborting a thread should be avoided. A thread should always stop by itself by completing its function
GetApartmentState
GetData
Interrupt
Join - in this case the calling thread waits until the other threads have finished
Resume (obselete, not used)
SetApartmentState - sets the apartment state of a thread before it is started
SetData
Sleep - the thread transitions to the WaitSleepJoin state. A thread can only put itself to sleep (another thread cannot do it)
Suspend (obselete, not used)
Yield


Properties

CurrentContext
CurrentThread
IsAlive
IsBackground
IsThreadPoolThread
Name
Priority
ThreadState


A managed thread can create a single threaded apartment that allows only one thread or a multi-threaded apartment that allows more than one thread.
You can control which type of apartment will be used in a thread by specifying the ApartmentState property before the thread is started.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext