.NET
  Home arrow .NET arrow Page 12 - 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 - Events


    (Page 12 of 15 )

    Although preventing access to a critical block of code is an important task when writing multithreaded applications, there is also the need to be able to signal a waiting thread. It is common to have one or more threads produce work for other threads to manipulate. Threads that create work are referred to as producer threads. Consumer threads are those that do the actual work handed out by the producer threads.

    The signal used by the .NET Framework is known as an event. Two types of events are available—AutoResetEvent and ManualResetEvent. When an event is set, it is signaled. Similarly, when an event is reset, the state of the event is not signaled. Events are automatically reset by the .NET Framework when exactly one thread is released when waiting on the event. Manually reset events must be cleared programmatically, and it is likely for multiple threads to be released. Listings 14.12 and 14.13 show the class definition of both types of events.

    Listing 14.12 Declaration of the System.Threading.ManualResetEvent Class

    System.Threading.ManualResetEvent = class (System.Threading.WaitHandle)
    public
    constructor Create(initialState: Boolean);
    procedure Close; virtual;
    function WaitOne: Boolean; virtual; overload;
    function WaitOne(timeout: TimeSpan; exitContext: Boolean) :
      Boolean; virtual; overload;
    function WaitOne(millisecondsTimeout: Integer; exitContext: Boolean) :
       Boolean; virtual; overload;
    function Reset: Boolean;
    function Set: Boolean;
    class function WaitAll(waitHandles: WaitHandle[];
      millisecondsTimeout: Integer; exitContext: Boolean) : Boolean; overload; static;
    class function WaitAll(waitHandles: WaitHandle[]; timeout: TimeSpan;
      exitContext: Boolean) : Boolean; overload; static;
    class function WaitAll(waitHandles: WaitHandle[]) : Boolean; overload; static;
    class function WaitAny(waitHandles: WaitHandle[];
      millisecondsTimeout: Integer; exitContext: Boolean) : Integer; overload; static;
    class function WaitAny(waitHandles: WaitHandle[]; timeout: TimeSpan;
      exitContext: Boolean) : Integer; overload;
    class function WaitAny(waitHandles: WaitHandle[]) : Integer; overload; static;
    property Handle: System.IntPtr read; write;
    end;

    Listing 14.13 Declaration of the System.Threading.AutoResetEvent Class

    System.Threading.AutoResetEvent = class (System.Threading.WaitHandle,
    IDisposable)
    public
    constructor Create(initialState: Boolean);
    function Reset: Boolean;
    function Set: Boolean;
    end;

    Using either type of event is identical. Use the Set() method to signal an event and the Reset() method to clear the signal. Notice that several methods are used for waiting on an event. The overloaded WaitOne() method waits for only one event, whereas the WaitAll() method waits for every entity to be signaled, and the WaitAny() method allows for waiting for any one signal to be fired. Finally, both the WaitAll() and WaitAny() methods provide the capability of waiting for a Mutex or either type of event. Recall that the WaitAll() and WaitAny() methods are inherited from WaitHandle and are listed in the earlier section "The System.Threading.WaitHandle Class."

    Find the code on the CD: \Code\Chapter 14\Ex06\prodcons.dpr for an example demonstrating how to use Events. Be sure to define the USE_EVENTS symbol.

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek