views:

302

answers:

5

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

+1  A: 

When the timer disables itself and sets the AnimationRunning variable to False, you could just call the method that should be executed next.

Smasher
I don't know which method should be called next. There are several methods that need to pause and wait for an animation to finish.
Svein Bringsli
A: 

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.

Lars Truijens
That sound like a good suggestion. Unfortunately I'm still on 2007 :-( (for a couple of months more, anyway)
Svein Bringsli
This also works without anonymous methods. It just takes a bit more code then :)
Lars Truijens
A: 

So what your timer is doing is something lengthy, on the order of several seconds? That kind of long-running activity shouldn't be performed in the main GUI loop anyway. Nor should waiting for such:

While you're waiting for the animation to finish (if you could), the rest of your application will behave like a dead program, i.e. it will not respond to the GUI in any way, including resizing, redrawing, closing with the X, etc.

The solution is to instead break up your DoStuff method into two; one that starts up the timer activity, and a second one that executes when the timer finishes. To accomplish the latter, that timer should be calling your second method just before saying goodbye.

Lars has done a great job of putting together an example; consider this the book to go with his movie :)

Carl Smotricz
Not terribly lengthy. A second or so. It's actually a small implementation of Monopoly. The animations are things like animating the die-roll, moving the tokens etc. There's no fancy graphics, only text and colored rectangles on a canvas, but a certain amount of animation makes the game feel better.I have one object, called TGame, that has a property called FDisplay, which is the frame I mentioned. The frame has methods like AnimateDieRoll and MoveToken.
Svein Bringsli
A: 

Make AnimateResults take a parameter of the method to be called when it's done.

Loren Pechtel
+1  A: 

A fellow Slovenian Delphi programmer wrote just the code you're looking for - Active Sleep.

gabr