Threading in Delphi for .NET - The System.Threading Namespace
(Page 3 of 15 )
The .NET Framework has a rich collection of classes and enumerations that are needed for writing multithreaded applications, which are located in the System.Threading namespace.
The System.Threading.Thread Class
Directly inheriting from the System.Object class, the Thread class provides the necessary methods for creating, aborting, suspending, and resuming threads. In addition, several properties exist for controlling the priority and determining other useful information. Listing 14.1 contains a partial definition of the Thread class.
Listing 14.1 Declaration of the System.Threading.Thread Class
System.Threading.Thread = class(System.Object)
public
constructor Create(start: ThreadStart);
procedure Start;
// terminate a thread
procedure Abort(stateInfo: System.Object); overload;
procedure Abort; overload;
// cancel an abort request
class procedure ResetAbort; static;
procedure Suspend;
procedure Resume;
// wakes a sleeping thread
procedure Interrupt;
// wait for a thread to finish
procedure Join; overload;
function Join(millisecondsTimeout: integer) : Boolean; overload;
function Join(timeout: TimeSpan) : Boolean; overload;
class procedure Sleep(millisecondsTimeout: Integer); overload; static;
class procedure Sleep(timeout: TimeSpan); overload; static;
// forces the thread to spin in a loop for a given number of iterations
class procedure SpinWait(iterations: Integer); static;
// thread local storage
class function AllocateDataSlot: LocalDataStoreSlot; static;
class function AllocateNamedDataSlot(name: String) : LocalDataStoreSlot; static;
class function GetNamedDataSlot(name: String) : LocalDataStoreSlot; static;
class procedure FreeNamedDataSlot(name: String); static;
class function GetData(slot: LocalDataStoreSlot) : System.Object; static;
class procedure SetData(slot: LocalDataStoreSlot; data: System.Object); static;
class function GetDomain: AppDomain; static;
class function GetDomainID: integer; static;
property Priority: System.Threading.ThreadPriority read; write;
property IsAlive: Boolean read;
property IsThreadPoolThread: Boolean read;
class property CurrentThread: System.Threading.Thread read;
property IsBackground: Boolean read; write;
property ThreadState: System.Threading.ThreadState read;
property ApartmentState: System.Threading.ApartmentState read; write;
// culture information
property CurrentUICulture: System.Globalization.CultureInfo read; write;
property CurrentCulture: System.Globalization.CultureInfo read; write;
// used with role based security
property CurrentPrincipal: System.Security.Principal.IPrincipal read; write;
property Name: System.String read; write;
end;
In particular, one property worth noting is the Name property. It can only be written to one time. Any attempt to write to the Name property more than once results in an exception.
Note - Notice that the Thread class contains methods to Suspend() and Resume() threads. Randomly suspending threads is not a good idea because it would be very easy to pause a thread during an inappropriate time. Imagine the results of suspending a thread when a lock is being held or in the middle of some file operation. The bottom line is that only the thread itself should call Suspend() because it knows the best places within the code to pause the thread. Calling Resume() on a suspended thread must be done from another thread.
Creating a thread using the Thread class is accomplished in one of two manners. The most frequent method is to use an instance method of a class. Another alternative is to use a static class method. These two methods of creating threads are referred to as manually created threads.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Delegates in Delphi >>
More .NET Articles
More By Xavier Pacheco