Threads

A thread is a unit of scheduling
Threads typically represent multiple flows of execution in an application (or process)
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();


STA - Single Threaded Apartment
COM provides threading guarantees for classes


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
GetApartmentState
GetData
Interrupt
Join
Resume
SetApartmentState - sets the apartment state of a thread before it is started
SetData
Sleep
Suspend
Yield


Properties

CurrentContext
CurrentThread
IsAlive
IsBackground
IsThreadPoolThread
Name
Priority
ThreadState


Apartments

An apartment is a logical container within a process
If you want to access a COM object from a thread, that thread must belong to an apartment.
.NET Framework does not use apartments.
COM classes do use apartments.


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.


ApartmentState
Unknown - the property has not been set
STA
MTA




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