Root > Reference > All Classes > TThreadEx > Methods > TThreadEx.Execute

Method TThreadEx.Execute

Previous pageReturn to chapter overviewNext page   

Defines thread's execution.

 

Unit

EBase

 

Syntax

 

Code (Delphi)

protected

procedure Execute;

reintroduce;

virtual;

abstract;

 

Remarks

Override Execute and insert the code that should be executed when the thread runs. Execute is responsible for checking the value of the Terminated property to determine if the thread needs to exit.

 

Important!

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

 

A thread executes when Create is called if CreateSuspended set to False, or when Start is first called after the thread is created if CreateSuspended set to True.

 

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;

 

Since analyzing 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_method_ebase_tthreadex_execute.php