?
Solved

Constructor and destructor with TThread

Posted on 2001-06-04
2
Medium Priority
?
1,590 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 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

862 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