Root > Reference > All Classes > TThreadEx

Class TThreadEx

Previous pageReturn to chapter overviewNext page   

Abstract class to create separate threads of execution.

 

Unit

EBase

 

Syntax

 

Code (Delphi)

TThreadEx = class(TThreadHook);

 

Remarks

TThreadEx is EurekaLog's replacement for TThread class. TThreadEx offers two additional arguments in constructor, which allows you to control thread's name and EurekaLog's state in this thread.

 

Create a descendant of TThreadEx to represent an execution thread in a multithreaded application. Each new instance of a TThreadEx descendant is a new thread of execution. Multiple instances of a TThreadEx derived class make an application multithreaded.

 

When an application is run, it is loaded into memory ready for execution. At this point it becomes a process containing one or more threads that contain the data, code and other system resources for the program. A thread executes one part of an application and is allocated CPU time by the operating system. All threads of a process share the same address space and can access the process's global variables.

 

Define the thread object's Execute method by inserting the code that should execute when the thread is executed.

 

Important!

You must call the inherited implementation in your overridden Execute method.

 

Methods

Methods

Properties

Properties

protected

Protected members

public

Public members

 

Examples

 

Code (Delphi)

type

TMyThread = class(TThreadEx)

protected

procedure Execute; override;

end;

 

procedure TMyThread.Execute;

begin

inherited; // - this is required

 

// ... your code ...

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

Thread: TMyThread;

E: TObject;

begin

// Create thread

Thread := TMyThread.Create('Thread Name');

try

 

// Wait for thread's completion.

// This wait can be implemented in any other way.

// E.g. you can assign OnTerminate handler;

// or you can PostMessage from thread to main thread.

Thread.WaitFor;

 

// Analyze thread completion.

// Re-raise any thread error in current thread.

// You should do this only after the thread has finished.

E := Thread.FatalException;

if Assigned(E) then

begin

// clear FatalException property

PPointer(@Thread.FatalException)^ := nil;

raise E;

end;

 

finally

FreeAndNil(Thread);

end;

end;

 

Threads launched with TThreadEx class will be EurekaLog-enabled by default. You can supply optional Boolean argument for TThreadEx's constructor to disable EurekaLog in thread, for example:

 

Code (Delphi)

Thread := TMyThread.Create('Thread Name', False { disable EurekaLog in thread } );

 

Of course, you can use the same approach while supplying Suspended argument, for example:

 

Code (Delphi)

Thread := TMyThread.Create(

False { create thread running },

'Thread Name',

False { disable EurekaLog in thread } );

 

Thread := TMyThread.Create(

True { create thread suspended},

'Thread Name',

True { enable EurekaLog in thread } );

 

TThreadEx class also supports anonymous threads:

 

Code (Delphi)

type

TForm1 = class(TForm)

...

private

procedure HandleExceptionInThread(Sender: TObject);

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

Thread: TThreadEx;

begin

Thread := TThreadEx.CreateAnonymousThread(

procedure

begin

raise Exception.Create('Test');

end,

'My thread');

Thread.OnTerminate := Self.HandleExceptionInThread;

Thread.Start;

Thread := nil; // never access thread var with FreeOnTerminate after Start

// All anonymous threads are marked with FreeOnTerminate by default

end;

 

procedure TForm1.HandleExceptionInThread(Sender: TObject);

var

Thread: TThread;

begin

Thread := (Sender as TThread);

if Assigned(Thread.FatalException) then

HandleException(Thread.FatalException);

end;

 

Since analyzing Thread.FatalException property or creating OnTerminate handler just for handling exceptions in thread is a lot of work - TThreadEx class presents a new property to handle exceptions automatically:

 

Code (Delphi)

type

TMyThread = class(TThreadEx)

protected

procedure Execute; override;

end;

 

procedure TMyThread.Execute;

begin

inherited; // - this is required

 

// ... your code ...

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

Thread: TMyThread;

begin

Thread := TMyThread.Create(True, 'My thread');

Thread.AutoHandleException := True;

Thread.FreeOnTerminate := True;

Thread.Start;

Thread := nil; // never access thread var with FreeOnTerminate after Start

end;

 

See also

SetEurekaLogStateInThread Function
NameThread Function



Send feedback... Build date: 2018-11-26
Last edited: 2018-11-26
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/topic_class_ebase_tthreadex.php