.NET
  Home arrow .NET arrow Page 4 - 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 - Delegates in Delphi


    (Page 4 of 15 )

    Regardless of the method used for manually created threads, both make use of delegates. A delegate is a .NET term for an object-oriented callback method that is type-safe. Similar to event handlers in Delphi, delegates provide a mechanism for multiple objects to be notified when called. Listings 14.2 and 14.3 demonstrate how delegates are used in Delphi when creating a thread.

    Listing 14.2 Creating Threads Using Instance Methods

    1:  program instancethreads;
    2:  {$APPTYPE CONSOLE}
    3: 
    4:  //
    5:  // This example demonstrates how to use native .NET methods to create a
    6:  // thread on a class with an instance method.
    7:  //
    8: 
    9:  uses
    10:  System.Threading;
    11:
    12: type
    13:  D4DNInstanceThread = class
    14:  private
    15:   FStartNumber : integer;
    16:  public
    17:   // ThreadMePlease will be executed on a different thread
    18:   procedure ThreadMePlease;
    19:   property StartNumber : integer write FStartNumber;
    20:  end;
    21:
    22: procedure D4DNInstanceThread.ThreadMePlease;
    23: var
    24:  stop : integer;
    25:  curNum : integer;
    26: begin
    27:  curNum := FStartNumber;
    28:  stop := FStartNumber + 10;
    29:  while curNum < stop do
    30:  begin
    31:   writeln('Thread ', System.Threading.Thread.CurrentThread.Name ,
    32:       ' current value is ',curNum);
    33:   inc(curNum);
    34:   Thread.Sleep(3);
    35:  end;
    36: end;
    37:
    38: var
    39:  ThreadWork1 : D4DNInstanceThread;
    40:  ThreadWork2 : D4DNInstanceThread;
    41:  Thread1 : Thread;
    42:  Thread2 : Thread;
    43: begin
    44:  writeln('Starting threading instance method example...');
    45:
    46:  // Create D4DNInstanceThread instance
    47:  ThreadWork1 := D4DNInstanceThread.Create;
    48:  ThreadWork1.StartNumber := 10;
    49:
    50:  // Create the thread, specifying the instance method to execute
    51:  Thread1 := Thread.Create(@ThreadWork1.ThreadMePlease);
    52:  Thread1.Name := 'one';
    53:
    54:  // Create another instance of D4DNInstanceThread
    55:  ThreadWork2 := D4DNInstanceThread.Create;
    56:  ThreadWork2.StartNumber := 100;
    57:
    58:  // Create the second thread, specifying the instance method to execute
    59:  Thread2 := Thread.Create(@ThreadWork2.ThreadMePlease);
    60:  Thread2.Name := 'two';
    61:
    62:  // Finally start the threads
    63:  Thread1.Start;
    64:  Thread2.Start;
    65:
    66:  // Wait for the two threads to finish
    67:  Thread1.Join;
    68:  Thread2.Join;
    69:
    70:  // Wait for the user to see the results
    71:  writeln('Done');
    72:  readln;
    73: end.

    Note: Find the code on the CD: \Code\Chapter 14\Ex01\. 

    Listing 14.2 demonstrates how to create a thread using an instance method. Any instance method that does not have any parameters can be executed on a thread. Look at the Thread.Create() constructor call (shown in Listing 14.2 on line 51). The parameter to the Thread.Create constructor is the address of the ThreadMePlease method. Under the covers, the Delphi compiler is creating a ThreadStart delegate. Other languages, such as C#, require a few more lines of code to accomplish the same task.


    Note - Although the ThreadStart delegate does not allow for passing parameters, using the instance method provides the opportunity to use either the constructor or properties to pass additional information needed by the thread. Listing 14.2 demonstrates this by setting the StartNumber.


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