Search code examples
delphiindy

How can we do an IdFTP.Get without blocking the UI?


I am browsing and listing content of a small FTP folder using IdFTP.

I would like that with a double click the user could be able to start the download of the file.

I noticed that FTP.Get is working well but is UI-blocking.

Could you point me to a possibile workaround?


Solution

  • Doing any UI-blocking operations in the main UI thread is never a good idea, always perform them in a worker thread instead, and have it synchronize with the UI thread as needed. Delphi has a TThread class for this purpose, which you can derive from and override its virtual Execute() method as needed. Or, use its CreateAnonymousThread() method.

    Indy also has its own TIdThread extension to TThread, as well as a TIdThreadComponent component wrapper, too.

    But, if you must use the main UI thread, then Indy does have a TIdAntiFreeze component that allows the main UI thread to continue processing messages while a UI-blocking Indy operation is in progress. Simply place a TIdAntiFreeze onto your MainForm (or instantiate it in code as needed) and Indy will handle the rest.

    Note that TIdAntiFreeze is basically just a wrapper for Application.ProcessMessages(), allowing Indy operations to pump the message queue periodically, so this comes with all the same re-entrance risks that are common with ProcessMessages(), so use this with care.