-1

I want to show a (animated) GIF in a TImage (GIF) while the main programming is running. Right now, either the main program waits for the GIF, or the GIF is waiting for the main program (thread) before showing.

My idea is that the GIF Image should be run in a separate thread. The GIF image is not used elsewhere in the main program. I have read a lot about multi-threading (new to me). Delphi allows me to build a new thread in a separate unit (a built-in feature), but many on-line examples and tutorials just put the TThread object on the main form. If I do that I get a compiler error that my mainform does not contain a member named 'Execute' See code).

What am I doing wrong? Should I go for the separate thread in the second unit? Any advise on how to access this GIF (TImage) via this separate thread? I do know that I have to take care of synchronization between the VCL and the separate thread. I also read that modern Delphi has a Queue possibility, claimed to be superior over synchronization.

(in the interface section):

type
  TThreadGIF = class(TThread)
  protected
    procedure Execute; override;
  end;

(in the implementation section, below the {$R *.dfm}):

procedure TThreadGIF.Execute;
begin
  NameThreadForDebugging('ThreadGIFname');
  { code should start here }
end;

If anybody knows a reference to a good tutorial, would also help.

How To Build Multithreaded Applications brings me in my current position.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Fred
  • 23
  • 4
  • inverse the logic the hard work on the thread side and the gif on the main thread side, you will avoid so many problems – Nasreddine Galfout Apr 30 '19 at 11:52
  • Form element can "run" only in same thread with form. And ... you don't need separate thread for image, you need separate thread for processing to not freeze UI. – Olvin Roght Apr 30 '19 at 11:53
  • 1
    A good gif viewer will spawn a timer and avoid all the complexity of threads. – David Heffernan Apr 30 '19 at 12:08
  • 1
    You can't ask for a reference to a tutorial here. See [What topics can I ask about here?](http://stackoverflow.com/help/on-topic) There are also literally dozens of existing questions here about updating the UI from a thread, all of which tell you that you can't do that except through `Synchronize` or `Queue`, both of which do the updating through the main thread. For your example of creating a thread properly, use `File->New->Other->Thread Object` from the IDE's main menu, which will generate a new thread unit with the proper format and a large comment block which you should read. – Ken White Apr 30 '19 at 12:18
  • How about using the built in `GIF` support in unit `Vcl.Imaging.GIFImg` [Docs here](http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.Imaging.GIFImg) (look for gif renderer) – Tom Brunberg Apr 30 '19 at 14:05

1 Answers1

3

You don't need a thread at all. The VCL's existing TGIFImage class, which can be used to display GIF images in a TImage component, already supports animated GIFs.

Look at the following TGIFImage properties:

Animate

Specifies whether the GIF should be animated.

The Animate property specifies whether the GIF stored in the TGIFImage instance should be animated when displayed.

AnimateLoop

Specifies whether the GIF animation should loop.

The AnimateLoop property specifies whether the animation of the GIF stored in the TGIFImage instance should loop.

Note: Changing the AnimateLoop property only has effect if the Animate property is set to True.

AnimationSpeed

Specifies the GIF animation speed.

The AnimationSpeed property specifies the GIF animation speed in percents of the normal speed.

The value of AnimationSpeed must be in the range from 0 through 1000.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Thanks for the reactions. My problem remains .. I think. The code I use is: Try ImGif.Picture.LoadFromFile(T); (ImGif.Picture.Graphic as TGIFImage).Animate := true; (ImGif.Picture.Graphic as TGIFImage).AnimationSpeed := 100; Except ShowMessage('Loading new picture failed'); End; the problem is not that the GIF isn't showing or is not animated, it is the when it appears (as explained - it only shows when the operating system decides it is time to show). – Fred May 01 '19 at 12:42
  • @Fred as it should be, since that is how UIs work (and no, you did NOT make that clear in your question), so what is the ACTUAL problem you are trying to solve? Please be more specific. Please update your question with the actual code you are trying to fix. – Remy Lebeau May 01 '19 at 14:42
  • 1
    Hi all, found the cause of my problem. The hick-ups and flickering I get when running an animated GIF are caused by my settings: I had used a fixed GIFimage and set the _proportional_ property to true. So the GIF size is adapted during runtime to fit image frame. Now I have set this property to false and the _autosize_ property to true. All problems solved! If a GIF is too large to fit I use freeware (e.g. look for EZGIF) to crop or resize to accomodate whatever size you allow for in your application. Maybe it is trivial, but I never saw this mentioned anywhere. Thanks all for advising! – Fred Jul 09 '19 at 12:05