title: Multithreaded WinHttp downloads
body: <p>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.</p>
  
  <p>The current software model uses <code>TThread</code> components. The GUI calls a <code>TDownloadThread</code> which then spawns the <code>TDownloadPartThreads</code> - these are the threads which actually do the downloading over 'WinHttp'.</p>
  
  <p><strong>My problem</strong>: The CPU is used up, even for one download where there are only 4 threads downloading.</p>
  
  <p><strong>My Supposed Causes:</strong></p>
  
  <ol>
  <li>I am writing to the destination file every 8192 bytes, and was
  wondering if I should buffer it before writing in one block?</li>
  <li>The thread communication is done via <code>Synchronize(MainForm.UpdateProgress(Downloaded, TotalSize))</code> 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?</li>
  </ol>
  
  <p><strong>My Solutions</strong></p>
  
  <ol>
  <li><p>Stagger the file writing and only write every <code>x</code> bytes.</p></li>
  <li><p>Update the <code>TThread</code> components to use <code>OmniThreadLibrary</code> and send the data back to the main form somehow. Each <code>TDownloadPart</code> thread would then become an <code>IOmniWorker</code> 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: <code>ProgressBar1.Position := sharedDataObject.Progress;</code></p></li>
  </ol>
  
  <p>Hopefully someone can point me in the right direction!</p>