To define the thread object

1 â–  Choose File ^ New ^ Other. The New Items dialog displays.

  1. On the New page, double-click Thread Object. The New Thread Object dialog displays.
  2. Enter a class name, for example, TMyThread.
  3. Optionally check the Named Thread checkbox, 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.

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 Unitl;

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

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 SetName, and adds the record TThreadNameinfo. The name is assigned to the ThreadNameinfo.FName field in the record, as shown here:

unit Unitl;

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

Forml.Caption := 'Updated in a thread';

{$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}

procedure TMyThread.SetName;

{$IFDEF MSWINDOWS}

ThreadNameInfo: TThreadNameInfo; {$ENDIF} begin

{$IFDEF MSWINDOWS}

ThreadNameInfo.FType := $1000; ThreadNameInfo.FName := 'MyThreadName'; ThreadNameInfo.FThreadID := $FFFFFFFF; ThreadNameInfo.FFlags := 0;

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

procedure TMyThread.Execute; begin

SetName;

{ Place thread code here } end;

end.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment