.NET
  Home arrow .NET arrow Page 3 - Threading in Delphi for .NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Threading in Delphi for .NET
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2004-08-30

    Table of Contents:
  • Threading in Delphi for .NET
  • Threading
  • The System.Threading Namespace
  • Delegates in Delphi
  • Creating Threads Using Static Methods
  • Threading Priority
  • Apartment State and Thread Pooling Class
  • Timer Classes
  • Writing Thread-safe Code .NET Style
  • Mutex and Monitor Classes
  • Locks that Distinguish Between Readers and Writers
  • Events
  • Thread Local Storage
  • User Interface Issues
  • Threading Exceptions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More .NET Articles
    More By Xavier Pacheco


     

    .NET ARTICLES

    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway