Recursively calling Move is a very bad idea (tm).
 
There are lots of ways of doing things but for this sort of application you probably want to write your own control loop which handles any windows messages (by occasionally calling Application.ProcessMessages) and also handles any updating which you want to perform (such as moving the objects around at particular intervals). Have a look at code like Application.Run and TForm.ShowModal (both in Forms.pas) to get some ideas of how to do this sort of thing.
 
David.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]On Behalf Of Gajo Istvan
Sent: Wednesday, 14 January 2004 8:37 AM
To: [EMAIL PROTECTED]
Subject: [DUG] READ THIS TOO!

I've just worked on the problem a bit more. I think it definitively needs tobe done by threading. The code looks kind of like this:
 
procedure TForm1.Button1Click(Sender: TObject);
var
 m: TMoving;
 x,y,angle: integer;
begin
 m := TMoving.Create;
 m.ParentWindow := Handle;
 ShowM(m);   // makes the bitmap visible
 GetXY(x,y,angle);   // gets the parameters from another source
 Move(x,y,angle,m,GetTickCount)  // following procedure..
end;
 
procedure TForm1.Move(x,y,angle: integer; M: TMoving; time: integer);
begin
 if time = GetTickCount then begin
  CalcXY(x,y,angle);   // gets new coordinates
  UpdateM(M);          // updates bitmap coordinates
  Move(x,y,angle,M,GetTickCount+2)  // call itself with new time
 end else
  Move(x,y,angle,M,time)   // call itself with same time
end;
 
So, until time = GetTickCount the procedure will call itself. The trouble is that since my processor probably does a few million jobs in one milliseconds, the procedure will be called so much that a stack overflow will happen. The procedure will never get the chance to call itself again in 2 ms.
 
This could be solved if the calling procedure would be removed from the stack after the new one was called. How to do this?
 
Reading the help file I found out that TThread has something like activating on a signal, so this would probably be a better solution. However, as I already mentioned, I don't know how to use threads. :(
 
Gajo
_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to