Multithreading – operations in a separate TThread block GUI thread
I use this tutorial http://delphi.about.com/od/kbthread/a/thread-gui.htm Creates a class that uses the tdownloadurl to asynchronously download files from the Internet in another thread I do this because I want to download files without blocking the UI thread, so the program will not be unresponsive during large downloads, and the progress bar can be updated, etc
I have a problem because even if I have finished downloading in another thread (inheriting from TThread and executing in the execute method), the GUI thread seems to be blocked and will not process messages until the download is completed This is my class code: http://codepad.org/nArfOPJK It has only 99 lines, a simple class I'm executing it, in the event handler for the button click:
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 is suitable for me to manually call app from the updatedownloadprogress method of class tdownloadthread Time of processmessages This makes the GUI unresponsive, but it makes the progress bar behave badly (aero's progress bar moves and lights up too fast), so I deleted it I want to solve this problem correctly. If I have to call processmessages, multithreading is meaningless
Can someone help me solve this problem? thank you.
Solution
I have a solution now!
Call tdownloadurl Execute (you call dl.Execute in TDownloadThread) causes the operation to be transferred back to the main thread, which is why your UI becomes unable to respond.
Instead, you should call executetarget (NIL), which does not execute such a conspiracy and works according to your intention: the download runs on the worker thread