Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait for a TTimer to finish?

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 :-( )

like image 429
Svein Bringsli Avatar asked Dec 30 '25 02:12

Svein Bringsli


1 Answers

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.

like image 110
Lars Truijens Avatar answered Dec 31 '25 15:12

Lars Truijens