0

With Delphi XE2, how can I play/update a TAnimate animation while a TIdHTTP object is working in the background? For example, I have IdHTTP1.Post waiting for this PHP "app" on a remote server to complete:

<?php
echo date('h:i:s') . "\n";
sleep(5);
echo date('h:i:s') . "\n";
?>

In these 5 seconds even a threaded timer (TJvThreadTimer) does not fire! I have also tried to put IdHTTP1.Post inside a separate thread using the OmniThreadLibrary with CreateTask. Even in this case the threaded timer does not fire, nor is the TAnimate animation updated.
So is there anything I can do to animate the animation while IdHTTP1.Post is working in the background?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 2
    Simply put the HTTP POST task to the background thread and play the animation in the main thread (on your form). Or am I missing something ? What do you precisely mean with update animation while the POST is working ? – TLama Jul 17 '13 at 19:04
  • @TLama The animation should simply PLAY, for the purpose to show to the user in a nice way that the program does not hang. But I HAVE put the HTTP POST in a background task, however the animation does not play. – user1580348 Jul 17 '13 at 19:16
  • Does the animation ever played (I mean in a plain project) ? Do you have Vista above Windows ? Are you going to show some of the Explorer animations ? If your answers to the above comment are no, yes and yes, then add `ShellAnimations` unit to your uses clause. – TLama Jul 17 '13 at 19:21
  • Of course I know the ShellAnimations thing, and the animation of course plays without the HTTP POST working. – user1580348 Jul 17 '13 at 19:22
  • 1
    Playing an animation in one thread gives little indication that actions in another thread haven't hung. To really prove that nothing's hung, the thread responsible for doing the real work needs to be the same thread responsible for advancing the animation. – Rob Kennedy Jul 17 '13 at 19:23
  • Indy has the `OnWork` event, so if you want to ensure your user that POST is in progress, do it from this event method. But that's all I can say about it, since I don't understand, what's the problem. – TLama Jul 17 '13 at 19:29
  • @RobKennedy Ok, you'r right, but that's a secondary question. The primary question is how to make the animation play while IdHTTP is working in the background. – user1580348 Jul 17 '13 at 19:30
  • How about a repro? Putting work in different thread should work. – David Heffernan Jul 17 '13 at 19:31
  • @TLama I've already tried the IdHttp.OnWork event, but it does fire only 2 times in these 5 seconds: Once at the beginning and once at the end. The problem is: The animation should play while IdHttp is working. – user1580348 Jul 17 '13 at 19:34
  • @user1580348: The `OnWork` event is only triggered when actual work is performed (bytes are sent or received). To do what you are asking for, you **must** call `TIdHTTP.Post()` in a separate thread than the one that is running the animation. If the animation is not working correctly in that scenario then something is wrong with your animation code. `TAnimate` uses window messages when UI Themes are enabled, so make sure you are not doing anything in your main thread code to block the main message loop from processing new messages while the `TIdHTTP` thread is working. – Remy Lebeau Jul 17 '13 at 19:41
  • It seems that the problem can EASILY be solved just by putting a TIdAntiFreeze component on the form (and setting its IdleTimeOut property to 10 instead of default 250). No threads needed, the animation just plays fine while IdHTTP.Post is working 5 seconds in the background! Very nice! – user1580348 Jul 18 '13 at 08:36
  • Please post that as an answer instead of just a comment. But note that if that component fixes your problem, there is probably more to your problem than you revealed in your question. – Rob Kennedy Jul 18 '13 at 11:59

1 Answers1

0

It seems that the problem can EASILY be solved just by putting a TIdAntiFreeze component on the form (and setting its IdleTimeOut property to 10 instead of default 250). No threads needed, the animation just plays fine while IdHTTP.Post is working 5 seconds in the background! Very nice!

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • That's really ugly workaround. You should have used worker thread for this task. And IMHO, whenever you use `TIdAntiFreeze`, there's something wrong in your design concept or in your code. – TLama Jul 19 '13 at 08:14
  • @TLama Thanks for your comment. Could you please explain why a worker thread should be used instead of `TIdAntiFreeze`? Thank you. – user1580348 Jul 20 '13 at 18:31