Solved

Constructor and destructor with TThread

Posted on 2001-06-04
2
1,501 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now