Solved

Constructor and destructor with TThread

Posted on 2001-06-04
2
1,539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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