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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting a delay into a thread

Status
Not open for further replies.

Benlecool

Programmer
Apr 14, 2005
11
CA
Hello,

I want to put a delay into a thread for n millisecunds. I can't use the sleep instruction because it stops all my application (all the threads). I've thought using a timer, but is there an equivalent instruction to ProcessMessages in a thread?
Thanks.
 
Benlecool,

sleep stops only the current thread, if it stops the whole app (it is no longer responding) then you are still in the main thread...

--------------------------------------
What You See Is What You Get
 
Hi,

Thanks answering. I'm sure I'm in my thread. I create it and put the priority to tpLower. Still my app is stopping when I put sleep in the thread.
Ben.
 
show us some code then,
maybe we help you to find the problem...

--------------------------------------
What You See Is What You Get
 
Here's the code in the thread:

procedure TAnimation.Execute;
begin
Synchronize(StartMoving)
end;


Constructor TAnimation.Create(Image: TImage; Steps, OrigineLeft, OrigineTop, FinalLeft, FinalTop: Integer);
Begin
FreeOnTerminate := True;
inherited Create(False);
TheImage:= Image;
TheStep := Steps;
TheLeft1:= OrigineLeft;
TheTop1:= Originetop;
TheLeft2 := FinalLeft;
TheTop2:= FinalTop;
End;


Procedure TAnimation.StartMoving;
var x, y: Integer;
begin
TheImage.Visible:=True;
If TheStep=0 then TheStep:=1;
For x:=1 To TheStep Do
Begin
TheImage.Visible:=False;
TheImage.Left:=TheLeft1+(x*(TheLeft2-TheLeft1) div x);
TheImage.Top:=TheTop1+(x*(TheTop2-TheTop1) div x);
TheImage.Visible:=True;
Sleep(50);
End;
End;

And Here's where I create the thread in the main code:

i:=Max(abs(TheImage.Left-370) div 30,abs(TheImage.Top-150) div 30);
TheThreadAnimation:=TAnimation.Create(TheImage, i, 370, 150, TheImage.Left, TheImage.Top);
TheThreadAnimation.Priority:= tpLower;


Thanks.
 
I suppose you want to do a sort of animation, and the image is placed onto the main form right?

one important rule when doing threaded animation : you need synchronization between the main thread and the animation thread, because the drawing takes place in the main thread.

in fact doing a simple animation, a simple TTimer would be enough. if you need a super responsive app and have a complicated animation, you can use a threaded timer like this one :


if you need to know more about threads, i can advise this link :
HTH,

Daddy


--------------------------------------
What You See Is What You Get
 
Sorry whosrdaddy, but most of the links are broken.
 
all three links worked for me......

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
again, all but 2 or 3 of the links on the homepages.borland worked for me. You might just wanto to search the Torry site.

Leslie

Anything worth doing is a lot more difficult than it's worth - Unknown Induhvidual
 
Benlecool,

my bad, that first link was indeed a CBuilder component.
anyway, a quick google search will reveal some components.

my point is : I don't think you need a thread in this case and your problem can be solved with a simple TTimer.
if you want to do FAST animations AND a responsive app, then maybe you'll need threads. remember that most games (even 3D games) are still single threaded...

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top