Defining Thread Objects

From RAD Studio
Jump to: navigation, search

Go Up to Defining thread objects Index

For most applications, you can use a thread object to represent an execution thread in your application. Thread objects simplify writing multi-threaded applications by encapsulating the most commonly needed uses of threads.

Note: Thread objects do not allow you to control the security attributes or stack size of your threads. If you need to control these, you must use the BeginThread function. Even when using BeginThread, you can still benefit from some of the thread synchronization objects and methods described in Coordinating Threads.

To use a thread object in your application (and to create a descendant of Classes.TThread):

  1. Choose one:
    • File > New > Other > Delphi Projects > Delphi Files > Thread Object
    • File > New > Other > C++ Projects > C++ Files > Thread Object
  2. In the New Thread Object dialog box, enter a class name, such as TMyThread.
  3. Check the Named Thread check box and enter a thread name (VCL applications only).
  4. Click OK. The Code Editor creates a new unit file to implement the thread.

For more information on naming threads, see Naming a Thread.

Note: Unlike most dialog boxes in the IDE that require a class name, the New Thread Object dialog box does not automatically prepend a 'T' to the front of the class name you provide.

Output

The automatically generated unit file that opens in the Code Editor contains the skeleton code for your new thread class. If you named your thread TMyThread, it would look like the following:

unit Unit1;
interface
uses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
type
  TMyTheadClass = class(TThread)
  protected
    procedure Execute; override;
  end;
implementation
 { 
 Important: Methods and properties of objects in visual components can only be
 used in a method called using Synchronize, for example,
     Synchronize(UpdateCaption);  
 and UpdateCaption could look like,
   procedure TMyTheadClass.UpdateCaption;
   begin
     Form1.Caption := 'Updated in a thread';
   end; 
   
   or 
   
   Synchronize( 
     procedure 
     begin
       Form1.Caption := 'Updated in thread via an anonymous method' 
     end
     )
   );
   
 where an anonymous method is passed.
 
 Similarly, the developer can call the Queue method with similar parameters as 
 above, instead passing another TThread class as the first parameter, putting
 the calling thread in a queue with the other thread.
   
}
{ TMyTheadClass }
procedure TMyTheadClass.Execute;
begin
  NameThreadForDebugging(mythreadname');
  { Place thread code here }
end;
end.


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(&UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TMyThreadClass::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------
__fastcall TMyThreadClass::TMyThreadClass(bool CreateSuspended)
 : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TMyThreadClass::Execute()
{
 NameThreadForDebugging("mythreadname");
 //---- Place thread code here ----
}
//---------------------------------------------------------------------------


Next Steps

In the automatically generated unit file, you

See Also