简体   繁体   中英

Animation of a Gif image is not working when execute a process

I have a Delphi project that consists of two forms namely MainForm and DialogForm . When I click on Button1 , the DialogForm should appear and stay on top until a process complete (the process takes a few seconds to complete).

The DialogForm includes a Timage component. When I click on the Button1 to show the DialogForm , the Gif image appears but without animation. This happens only when the process starts (without the process the animation works). What is the reason for this and how to keep the animation until closing the DialogForm ?

procedure TMainForm.Button1Click(Sender: TObject);
var
  gif: TGIFImage;
begin
  Enabled:=false;
  try
        DialogForm.Show;
        DialogForm.Refresh;

        // The process is:
         ...
        ipcAES1.Encrypt;//where ipcAES is part of the IPWorks Encrypt library
        RichEdit1.Text:=ipcAES1.OutputMessage;
    finally
        Enabled:= true;
        DialogForm.Close;
    end;

end;
//--------------------------------------- 
procedure TDialogForm.FormShow(Sender: TObject);
var
  gif: TGIFImage;
begin
  gif := TGIFImage.Create;
  gif.LoadFromFile('D:\preview.gif');
  gif.Animate := True;
    image1.Parent := Self;
    image1.Left := 0;
    image1.Top := 0;
    image1.width := 800;
    image1.height := 800;
    image1.Picture.Assign(gif);
    gif.Animate := True;
    gif.Free;  
end;

As said by many in this thread, because the processing is done in the main thread, the UI is not updated during this process.

To make sure the UI is updated while the process is running, let a separate thread do the processing:

procedure TForm1.Button1Click(Sender: TObject);
var
  aProcessingThread: TThread;
begin
  // First read all data needed by the process from UI controls (or other non-threadsafe parts)
  <data> := ...;

  // Then create a new (anonymous) thread with the code you need to run your process
  aProcessingThread := TThread.CreateAnonymousThread(
    procedure
    begin
      // create the objects you need to do the processing
      ipcAES1 := Txxx.Create;
      try
        // Set the data
        ipcAES1.<data> := <data>;

        // Execute the proces:
        // ...
        ipcAES1.Encrypt;

      finally
        // When the process is done, use 'Synchronize' to interact with the UI
        // again, so you can add the processed data to the RichtEdit and so on...
        TThread.Synchronize(nil,
          procedure
          begin
            // Now you can interact again with the UI
            RichEdit1.Text := ipcAES1.OutputMessage;
            Enabled:= true;
            DialogForm.Close;
          end);
        ipcAES1.Free;
      end;
    end);

  // The thread is now created, but not started/running, so you can now show
  // the dialog and then start the thread, at which point the ButtonClick event
  // exists, but the progress dialog is shown and the thread is running.
  Enabled := False;
  DialogForm.Show;
  aProcessingThread.Start;
end;

Of course this only a basic example of how to use an (anonymous) thread to do some processing in the background. Please note you need to handle Exceptions inside the thread (try/except).

A small tip regarding the TGifImage loading: you can just call Picture.LoadfromFile to load the gif as long as you include Vcl.Imaging.GIFImg in the uses clause.

procedure TForm1.FormShow(Sender: TObject);
begin
  image1.Picture.LoadFromFile('D:\preview.gif');

  image1.Parent := Self;
  image1.Left := 0;
  image1.Top := 0;
  image1.width := Image1.Picture.Width;
  image1.height := Image1.Picture.Height;

  (image1.Picture.Graphic as TGIFImage).Animate := True;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM