Solved

Constructor and destructor with TThread

Posted on 2001-06-04
2
1,514 Views
Last Modified: 2012-06-27
Hi,

I am creating my own thread class, by deriving a class from TThread.  Now I need to be able to add my own constructor (with my own parameters, used to initialise some data before the thread starts) and destructor (to clean up after it) to the class.

How can I add my own code to do this, without effecting the TThread's own constructor and destructor?  I still need the thread to run correctly when my Create is called (and run it with CreateSuspended being False).

Thanks in Advance,

Richard2000
0
Comment
Question by:Richard2000
2 Comments
 
LVL 1

Expert Comment

by:Baksa
ID: 6152676
Here is the Borland way, this is implementation of TThread descendant,  TServerAcceptThread.


TServerAcceptThread = class(TThread)
  private
    FServerSocket: TServerWinSocket;
  public
    constructor Create(CreateSuspended: Boolean; ASocket: TServerWinSocket);
    procedure Execute; override;

    property ServerSocket: TServerWinSocket read FServerSocket;
  end;



constructor TServerAcceptThread.Create(CreateSuspended: Boolean;
  ASocket: TServerWinSocket);
begin
  FServerSocket := ASocket;
  inherited Create(CreateSuspended);
end;

0
 
LVL 3

Accepted Solution

by:
MarcG earned 50 total points
ID: 6155555
Baksa said already  ... you can write your own constructor and sestructor like you would if you write an new class, the only thing you now have to do is calling the constructor/destructor of your parent class.

with constructor like this
constructor MyClass.Create(MyData: MyType, MyOwner: TComponent)
begin
  DoMyCode;
  inherited create(MyOwner);
  //First do your things than call the constructor of parent
end;

and the other way with destructor
destructor MyDestroy;
begin
  inherited destroy;
  //First call parent destructor
  FreeMyData;
end;

You can do this with all classes not only TThreads
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question