.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
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 - 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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - 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

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT