.NET
  Home arrow .NET arrow Page 10 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 - Mutex and Monitor Classes


    (Page 10 of 15 )

    The System.Threading.Mutex Class

    A mutex is a mutually exclusive object that acts similar to a lock. Locking the mutex is accomplished by calling one of the overloaded WaitOne() methods. Mutexes have the option of being named. Specifying a name allows for the mutex to be shared between AppDomains and processes. The mutex is unlocked by calling the ReleaseMutex() method. Listing 14.8 shows the methods declared in the Mutex class.

    Listing 14.8 Declaration of the System.Threading.Mutex Class

    System.Threading.Mutex = class (System.Threading.WaitHandle)
    public
    constructor Create(initiallyOwned: Boolean; name: String;
      var createdNew: Boolean); overload;
    constructor Create(initiallyOwned: Boolean; name: String); overload;
    constructor Create(initiallyOwned: Boolean); overload;
    constructor Create; overload;
    procedure ReleaseMutex;
    end;

    In addition to the Wait methods inherited from WaitHandle, the Mutex class adds several constructors as well as the ReleaseMutex() method. These constructors provide the ability to create, optionally lock (own), and optionally name the mutex. A mutex is unlocked by using the ReleaseMutex() method.

    The System.Threading.Monitor Class

    The System.Threading.Monitor class has been designed to be more lightweight than the Mutex class. It should be used when a high-performance locking mechanism is needed.

    Although the Monitor class looks similar to a mutex, there are some subtle differences. Take a look at Listing 14.9 for the definition of the Monitor class.

    Listing 14.9 Declaration of the System.Threading.Monitor Class

    System.Threading.Monitor = class (System.Object)
    public
    class procedure Enter(obj: System.Object); static;
    class function TryEnter(obj: System.Object) : Boolean; overload; static;
    class function TryEnter(obj: System.Object;
      millisecondsTimeout: Integer) : Boolean; overload; static;
    class function TryEnter(obj: System.Object;
      timeout: TimeSpan) : Boolean; overload; static;
    class function Wait(obj: System.Object; millisecondsTimeout: Integer;
      exitContext: Boolean) : Boolean; overload; static;
    class function Wait(obj: System.Object; timeout: TimeSpan;        
      exitContext: Boolean) : Boolean; overload; static;
    class function Wait(obj: System.Object;
      millisecondsTimeout: Integer) : Boolean; overload; static;
    class function Wait(obj: System.Object;
      timeout: TimeSpan) : Boolean; overload; static;
    class function Wait(obj: System.Object) : Boolean; overload; static;
    class procedure Pulse(obj: System.Object); static;
    class procedure PulseAll(obj: System.Object); static;
    class procedure Exit(obj: System.Object); static;
    end;

    Using the Enter() and Exit() methods of the Monitor has the same effect as locking with a Mutex's WaitOne() and ReleaseMutex() methods. The Monitor class also has TryEnter() methods that attempt to acquire a lock without waiting. An optional timeout parameter specifies how long to wait on the lock before giving up.

    Notice that all the methods of the Monitor class are class methods. This provides the flexibility to specify any object to lock upon.

    Finally, the Monitor class allows for signaling and waiting for a signal with the Wait() and Pulse() methods. Both Wait() and Pulse() methods require the Monitor to be locked—that is, surround the Wait() and Pulse() methods with an Enter() / Exit() pair.

    Find the code on the CD: \Code\Chapter 14\Ex06\prodcons.dpr for an example that demonstrates a thread-safe queue using the Mutex and Monitor classes.

    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

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek