RAD Studio
ContentsIndex
PreviousUpNext
Defining the Thread Object
To define the thread object

  1. Choose FileNewOtherDelphi ProjectsDelphi Files or FileNewOtherC++Builder Files and double-click the Thread Object icon. The New Thread Object dialog displays.
  2. Enter a class name, for example, TMyThread.
  3. Optionally check the Named Thread check box, and enter a name for the thread, for example, MyThreadName.
    Tip: Entering a name for Named Thread makes it easier to track the thread while debugging.
  4. Click OK.
The Code Editor displays the skeleton code for the thread object.  

The code generated for the new unit will look like this if you named your thread class TMyThread.

unit Unit1;

interface

uses
  Classes;

type
  TMyThread = class(TThread)
  private
    { Private declarations }
  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 TMyThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TMyThread }

procedure TMyThread.Execute;
begin
  { Place thread code here }
end;

end.

Adding a name for the thread generates additional code for the unit. It includes the Windows unit, adds the procedure (Delphi) or function (C++) SetName, and adds the record TThreadNameInfo (Delphi) or struct THREADNAME_INFO (C++). The name is assigned to the FName field, as shown here:

unit Unit1;

interface

uses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};

type
  TMyThread = class(TThread)
  private
    procedure SetName;
  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 TMyThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}

{ TMyThread }

procedure TMyThread.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'MyThreadName';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

procedure TMyThread.Execute;
begin
  SetName;
  { Place thread code here }
end;

end.

 

// Unit1.h

#ifndef Unit1H
#define Unit1H
#include <Classes.hpp>
class TMyThread : public TThread
{
  typedef struct tagTHREADNAME_INFO
  {
    DWORD dwType;     
    LPCSTR szName;    
    DWORD dwThreadID; 
    DWORD dwFlags;    
  } THREADNAME_INFO;
private:
  void SetName();
protected:
  void __fastcall Execute();
public:
  __fastcall TMyThread(bool CreateSuspended);
};

#endif

 

// Unit1.cpp
#include "Unit3.h"
#pragma package(smart_init)

__fastcall TMyThread::TMyThread(bool CreateSuspended)
  : TThread(CreateSuspended)
{
}

void TMyThread::SetName()
{
  THREADNAME_INFO info;
  info.dwType = 0x1000;
  info.szName = "TMyThreadName";
  info.dwThreadID = -1;
  info.dwFlags = 0;

  __try
  {
     RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD),(DWORD*)&info; );
  }
  __except (EXCEPTION_CONTINUE_EXECUTION)
  {
  }
}

void __fastcall TMyThread::Execute()
{
  SetName();
  //---- Place thread code here ----
}
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!