English 中文(简体)
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 :-( )

最佳回答

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.

问题回答

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

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

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

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





相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签