Operations in a separate GUI thread of a TThread block

I used this http://delphi.about.com/od/kbthread/a/thread-gui.htm tutorial to create a class that asynchronously downloads a file from the Internet to another thread using TDownLoadURL . I did this because I want to download the file without blocking the user interface stream, so that the program does not stop responding during large downloads, the progress bar could update, etc.

I have a problem because although I did the download in another thread (inheriting from TThread and doing the work in the Execute method), the GUI thread seems to be blocked and does not process messages until the download is complete. Here is the code for my class: http://codepad.org/nArfOPJK (it's just 99 lines, a simple class). I accomplish this through this, in an event handler for clicking a button:

 var frame: TTProgressFrame; dlt: TDownloadThread; begin dlt := TDownloadThread.Create(True); dlt.SetFile('C:\ohayo.zip'); dlt.SetURL('http://download.thinkbroadband.com/512MB.zip'); dlt.SetFrame(frame); dlt.SetApp(Application); dlt.Start; 

Note. The SetApp method was used when I manually called app.ProcessMessages from the UpdateDownloadProgress method of my TDownloadThread class. This will keep the GUI from immunity, but it led the progress bar to work quite oddly (the moving light-emitting light indication of the progress bar is too fast), so I deleted it. I want to fix this correctly, and if I need to call ProcessMessages , then there is no point in multithreading it.

Can someone help me fix this problem? Thanks.

+7
source share
1 answer

Now I have a solution for you!

Calling TDownLoadURL.Execute (your dl.Execute call on TDownloadThread ) causes the action to be passed back to the main thread, so your user interface becomes unresponsive.

Instead, you should call ExecuteTarget(nil) , which does not perform such fraud and works as you plan: loading is performed in the workflow.

+5
source

All Articles