I have a TFrame (fraDisplay) with a TTimer (timAnimateDataChange). The timer is used to control a small animation. In the form containing the frame I want to have a method that does something like this:
procedure TForm.DoStuff;
begin
DoSomeLogicStuff;
fraDisplay.AnimateResult;
WaitForAnimationToFinish;
DoSomeOtherLogicStuff;
fraDisplay.AnimateEndResult;
WaitForAnimationToFinish;
fraDisplay.Finalize;
end;
The animations are basically redraws of a TImage32, timed by a timer. The timer will disable it self when finished, and the frame has a boolean property called AnimationRunning which will be set to false when the animation is finished.
There are no threads or anything like that to complicate or help matters.
The question is, how do I implement the WaitForAnimationToFinish-method?
(Btw, this is not a good solution:
procedure TForm.WaitForAnimationToFinish;
begin
repeat
Application.ProcessMessages;
until not fraDisplay.AnimationRunning;
end;
since the timer won't fire while the method is running :-( )
Smasher's suggestion could be implemented using Delphi 2009's anonymous methods.
procedure TForm.DoStuff;
begin
DoSomeLogicStuff;
fraDisplay.AnimateResult.OnFinished :=
procedure (Sender: TObject)
begin
DoSomeOtherLogicStuff;
fraDisplay.AnimateEndResult.OnFinished :=
procedure (Sender: TObject)
begin
fraDisplay.Finalize;
end;
fraDisplay.AnimateEndResult;
end;
fraDisplay.AnimateResult;
end;
BTW: Actually, WaitForAnimationToFinish will let OnTimer fire, since it uses windows messages that are dispatched when calling ProcessMessages. But it is a bad idea anyway since it uses lots of CPU without actually needing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With