Search code examples
multithreadingdelphigiffreeze

TImage gif refresh during stuff


I have a form (BorderStyle = bsNone) with just a TImage as background. The TImage has a GIF loaded.

After calling Show(), I start a process where I use TSHFileOpStruct to copy file. When the copy is finished, the program terminates.

The problem is that, when the copy starts, the GIF is frozen until the process is finished. I would use TSHFileOpStruct because it's very efficient when there is a lot of files and big sizes.

Do you have any suggestion?

I have tried to use Application.Processmessages() before or after the execution of TSHFileOpStruct object, but nothing changes.


Solution

  • The reason everything freezes is because you are performing the file copy in the context of the main UI thread, blocking the message loop until the operation is finished. The UI, including the GIF, requires the message loop to remain active to handle painting and animation requests.

    You need to either:

    • move your copy logic to a separate worker thread, via TThread or TTask, so you are not blocking the main UI thread anymore.

    • switch to a different API, such as IFileOperation or CopyFileEx(), that allows to you receive status callbacks during the file copy. SHFileOperation() does not support that. This way, you can call TForm.Update() or Application.ProcessMessages() during the copying to keep your UI responsive.