.NET
  Home arrow .NET arrow Page 9 - 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 - Writing Thread-safe Code .NET Style


    (Page 9 of 15 )

    Writing a multithreaded application is pointless if multiple threads cannot interact in a predictable and bug-free manner. A body of code is said to be thread-safe if multiple threads can safely execute it without any side effects. One way of making a method or function thread-safe is to serialize access to it, thereby allowing only one thread to execute the code at a time.

    Thread-safe code can be accomplished by following a few guidelines:

    • Avoid variables and objects shared between threads. If this is not feasible, use a locking mechanism to serialize access.

    • Use variables declared on the stack (for example, local variables).

    • Use stateless routines by passing in all parameters that are needed to do its work.

    • Use stateless methods, performing work on the internal data of the object. (This assumes that each object instance is only accessible from one thread.) Any additional data should be passed as parameters. Make sure that parameters or fields do not refer to other global data or unprotected objects.

    • Use thread local storage (threadvar), which is explained in a later section of this chapter.

    Fortunately, the .NET Framework provides a rich collection of classes to help write thread-safe code. These classes are generalized as locking and event mechanisms. Locking performs serialization, whereas an event is used for communication between threads.

    Locking Mechanisms

    Serializing access to a resource is accomplished by using a locking mechanism. By only allowing one thread to enter into a protected region, other threads are locked out. When one thread exits a protected region of code, another thread is then allowed to enter it.

    There are three locking mechanisms in the .NET Framework: mutexes, monitors, and Read-Write locks. In addition, the Interlocked class provides a basic set of operations that are atomic. Atomic operations are those that are guaranteed not to be interrupted once they begin.

    The System.Threading.WaitHandle Class

    Before discussing mutexes and monitors, it is necessary to begin with the WaitHandle class as these two locking mechanisms inherit common functionality from it. Listing 14.7 contains the definition of the WaitHandle class.

    Listing 14.7 Declaration of the System.Threading.WaitHandle Class

    System.Threading.WaitHandle = class (System.MarshalByRefObject, IDisposable)
    public
    constructor Create;
    procedure Close; virtual;
    function WaitOne: Boolean; overload; virtual;
    function WaitOne(timeout: TimeSpan;
              exitContext: Boolean) : Boolean; overload; virtual;
    function WaitOne(millisecondsTimeout: Integer;
              exitContext: Boolean) : Boolean; overload; virtual;
    class function WaitAll(waitHandles: array of WaitHandle;
                 millisecondsTimeout: Integer;
                 exitContext: Boolean) : Boolean; overload; static;
    class function WaitAll(waitHandles: array of WaitHandle;
                 timeout: TimeSpan;
                 exitContext: Boolean) : Boolean; overload; static;
    class function WaitAll(waitHandles: array of WaitHandle) : Boolean;
      overload; static;
    class function WaitAny(waitHandles: array of WaitHandle;
                 millisecondsTimeout: Integer;
                 exitContext: Boolean) : Integer; overload; static;
    class function WaitAny(waitHandles: array of WaitHandle;
                 timeout: TimeSpan;
                 exitContext: Boolean) : Integer; overload; static;
    class function WaitAny(waitHandles: array of WaitHandle) : Integer;
      overload; static;
    property Handle: System.IntPtr read; write;
    end;

    Similar to the Win32 API WaitForMultipleObjects(), the overloaded WaitAll() method accepts multiple WaitHandle objects and will only return if all handles are signaled. The WaitAny() method returns if any of the multiple WaitHandle objects are signaled. Both methods have optional timeout parameters that enable the methods to exit the wait prematurely and avoid deadlocks.

    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 4 hosted by Hostway