Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to download 1000 files, starting 1000 threads at once is certainly not the best option. Not only it probably won't get you any speedup when compared with downloading just a few files at a time, it will also require at least 1 GB of virtual memory. Creating threads is expensive, try to avoid doing so in a loop.</p> <p>What you should do instead is to use <code>Parallel.ForEach()</code> along with the asynchronous versions of the request and response operations. For example like this (WPF code):</p> <pre><code>private void Start_Click(object sender, RoutedEventArgs e) { m_tokenSource = new CancellationTokenSource(); var urls = …; Task.Factory.StartNew(() =&gt; Start(urls, m_tokenSource.Token), m_tokenSource.Token); } private void Cancel_Click(object sender, RoutedEventArgs e) { m_tokenSource.Cancel(); } void Start(IEnumerable&lt;string&gt; urlList, CancellationToken token) { Parallel.ForEach(urlList, new ParallelOptions { CancellationToken = token }, url =&gt; DownloadOne(url, token)); } void DownloadOne(string url, CancellationToken token) { ReportStart(url); try { var request = WebRequest.Create(url); var asyncResult = request.BeginGetResponse(null, null); WaitHandle.WaitAny(new[] { asyncResult.AsyncWaitHandle, token.WaitHandle }); if (token.IsCancellationRequested) { request.Abort(); return; } var response = request.EndGetResponse(asyncResult); using (var stream = response.GetResponseStream()) { byte[] bytes = new byte[4096]; while (true) { asyncResult = stream.BeginRead(bytes, 0, bytes.Length, null, null); WaitHandle.WaitAny(new[] { asyncResult.AsyncWaitHandle, token.WaitHandle }); if (token.IsCancellationRequested) break; var read = stream.EndRead(asyncResult); if (read == 0) break; // do something with the downloaded bytes } } response.Close(); } finally { ReportFinish(url); } } </code></pre> <p>This way, when you cancel the operation, all downloads are canceled and no new ones are started. Also, you probably want to set <code>MaxDegreeOfParallelism</code> of <code>ParallelOptions</code>, so that you aren't doing too many downloads at once.</p> <p>I'm not sure what do you want to do with the files you are downloading, so using <code>StreamReader</code> might be a better option.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload