.NET
  Home arrow .NET arrow Page 5 - 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 - Creating Threads Using Static Methods


    (Page 5 of 15 )

    Listing 14.3 Creating Threads Using Static Methods

    1:  program staticthreads;
    2:  {$APPTYPE CONSOLE}
    3:  uses
    4:   System.Threading;
    5: 
    6: type
    7:  D4DNStaticThread = class
    8:  public
    9:   class procedure ThreadMePlease; static;
    10:  end;
    11:
    12: class procedure D4DNStaticThread.ThreadMePlease;
    13: var
    14:  stop  : integer;
    15:  curNum : integer;
    16:  rnd  : System.Random;
    17: begin
    18:  rnd  := System.Random.Create;
    19:  curNum := rnd.Next(1000);
    20:  stop := curNum + 10;
    21:  while curNum < stop do
    22:  begin
    23:   writeln('Thread ', System.Threading.Thread.CurrentThread.Name,
    24: 'current value is ', curNum);
    25:   inc(curNum);
    26:   // Randomly give up time-slice to other thread
    27:   if rnd.Next(100) < 50 then
    28:    Thread.Sleep(0);
    29:  end;
    30: end;
    31:
    32: var
    33:  thrd1 : Thread;
    34:  thrd2 : Thread;
    35: begin
    36:  Console.Writeline('Starting static method threading example...');
    37:
    38:  // create the thread passing the static method
    39:  thrd1 := Thread.Create(@D4DNStaticThread.ThreadMePlease);
    40:  thrd1.Name := 'one';
    41:
    42:  // create another identical thread
    43:  thrd2 := Thread.create(@D4DNStaticThread.ThreadMePlease);
    44:  thrd2.Name := 'two';
    45:
    46:  // start both threads
    47:  thrd1.Start;
    48:  thrd2.Start;
    49:
    50:  // wait until both threads have finished
    51:  thrd1.Join;
    52:  thrd2.Join;
    53:
    54:  Console.Writeline('Done');
    55:  readln;
    56: end.

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

    In Listing 14.3, a thread is created on a static class method. Similar to threading instance methods, any static class method without parameters can be executed on a thread.

    Listings 14.2 and 14.3 contain a subtle bug. The output written to the console is performed in an unsynchronized manner. It is very likely that the output between two threads will be mixed together. This is caused by the implementation of the writeln procedure by the Delphi compiler and the runtime library. Each parameter passed to the writeln procedure results in a separate call to either the Console.Write or Console.WriteLine method. Changing the writeln (on line 31 in Listing 14.2, and line 23 in Listing 14.3) to use Console.WriteLine instead or passing only one parameter to the writeln procedure (for example, using writeln(Format(..));) will produce the proper behavior.

    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