I am creating a Delphi application to download files from the Internet and if the server supports range requesting it will be multi threaded. The progress is also relayed back to the GUI.
The current software model uses TThread
components. The GUI calls a TDownloadThread
which then spawns the TDownloadPartThreads
- these are the threads which actually do the downloading over 'WinHttp'.
My problem: The CPU is used up, even for one download where there are only 4 threads downloading.
My Supposed Causes:
- I am writing to the destination file every 8192 bytes, and was wondering if I should buffer it before writing in one block?
- The thread communication is done via
Synchronize(MainForm.UpdateProgress(Downloaded, TotalSize))
which I have heard is AWFUL to do, maybe I should share an object between the threads so I can access this using a timer on the main form, to update progress?
My Solutions
Stagger the file writing and only write every
x
bytes.Update the
TThread
components to useOmniThreadLibrary
and send the data back to the main form somehow. EachTDownloadPart
thread would then become anIOmniWorker
and send back its progress by sharing an object with the main form. The main form would then use a timer to update its progress, like:ProgressBar1.Position := sharedDataObject.Progress;
Hopefully someone can point me in the right direction!