[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Constructor and destructor with TThread

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 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