Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi Constructors 3

Status
Not open for further replies.

gp4

Technical User
Sep 27, 2002
30
AU
Does anyone know if there is some good documentation/guidelines on editing Constructors.

Thanks
 
There's not much to it, just a couple of simple rules:

Always call Inherited at the beginning of the constructor.

Always initialize properties/internal objects - for example, if I have a TStringList as one of the "pieces" of my object, I'll initialize with

FMyStringList := TStringList.Create;

in the constructor.

In the destructor, free any "pieces" like the TStringList mentioned above.

Always call Inherited last.

-Dell
 
Hi Hilfy,
Thanks for your update.
I need some further help with this.

I'm trying to create a Timer object in a Thread.

Here's part of the code:

type
ttimernew = class(TTimer)
private
public
constructor Create (AOwner:TComponent); override;
procedure newevent (Sender:TObject);
protected
end;

implementation

constructor ttimernew.Create (AOwner:TComponent);
begin
inherited Create(AOwner);
OnTimer:=newevent;
end;
procedure ttimernew.newevent(Sender:TObject);
begin
{my code when the Ontimer event occurs}
end;
procedure tthreadtest.Execute;
var
mytimer:ttimernew;
begin
mytimer.Interval:=100;
mytimer.Enabled:=true;

end;

But I seem to get an Access Violation exception when the thread executes. I beleive its related to initialising mytimer.
Any ideas what should be done?

Thanks
 
Hi gp4,

your code will never work for 3 reasons :

1) you forgot to create the timer (hence the AV)
-> mytimer:=ttimernew.create(nil)
2) your thread will already be finished BEFORE your timer event WOULD fire. (if FreeOnTerminate = true)
3) threads by default don't have message pumps. since the TTimer object uses the WM_TIMER message, this will never work.

you can do 2 things to resolve this problem :

a) the hard way :
create a message queue and check for messages in your thread

Code:
procedure tthreadtest.ProcessMessages;

var  Msg : Tmsg;

begin
 if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
  begin

   if Msg.hwnd = 0 then
    begin

     case Msg.message of

      WM_USER_XXXX : ; handle own thread messages

     else
      DispatchMessage(Msg); // dispatch message to other handles
     end
    end
   else
    DispatchMessage(Msg);
  end;
end;


procedure tthreadtest.Execute;
var
  mytimer:ttimernew;
begin
 //init
 try
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE); { Create message queue } 
  mytimer:=ttimernew.create;
  mytimer.Interval:=100;
  mytimer.Enabled:=true;
 //thread loop
 while not terminated do
  begin 
   processmessages;
   sleep(20); // avoid 100% cpu time
  end;
 finally
 // cleanup 
  mytimer.enabled:=false;
  if assigned(mytimer) then freeandnil(timer);
 end;
end;

b) the easy way (recommended)

if you want to something every x milliseconds, the code can be very straightforward :
Code:
procedure tthreadtest.Execute;

begin
 //init
 try
 //thread loop
  while not terminated do
   begin 
    // do nothing for x milliseconds
    // avoid using large numbers for x since this will
    // make your thread unresponsive when Terminate is called (let's say, no longer than 1000 milliseconds)
    sleep(x); // avoid 100% cpu time
    DoStuff; // this is that would be in the timer event
   end;
 finally
 // cleanup 
 end;
end;

if you want longer timeouts, look at this :

Code:
procedure tthreadtest.Execute;

var Stime : TDateTime;

begin
 //init
 try
 //thread loop
  Stime:=Now;
  while not terminated do
   begin 
    sleep(20); // avoid 100% cpu time    
    if SecondsBetween(Now, Stime) > x then
     begin
      DoStuff;
      Stime:=Now;
     end;
   end;
 finally
 // cleanup 
 end;
end;

for longer timeouts, the SecondsBetween() function can offcourse be replaced by MinutesBetween() or DaysBetween() and so on...

hope this helps,

Daddy

--------------------------------------
What You See Is What You Get
 
I hope that link stays put for a while. I've bookmarked it under "delphi" as a valuable reference. Definately star worthy!

 
Thankyou whosrdaddy .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top