Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's not clear whether you want that loop to wait until all the objects have been processed, or if you want that loop to start the processing, queue the rest, and then continue while the workers do their processing.</p> <p>I'm going to assume the second.</p> <p>I'll say up front that there's probably a cleaner way to do this using async/await, but I'm not so good with those yet. But my proposal below will work.</p> <p>First, create a <code>BlockingCollection&lt;myObject&gt;</code>. That's a shared queue that your workers will use.</p> <p>Then, create 20 threads (either background worker or Task) that process the queue. Here's an example using just 2 background workers.</p> <pre><code>BlockingCollection&lt;myObject&gt; theQueue = new BlockingCollection&lt;myObject&gt;(); bgw1 = new BackgroundWorker(); bgw2 = new BackgroundWorker(); bgw1.RunWorkerAsync(); bgw2.RunWorkerAsync(); // now, populate the queue foreach (var obj in myObjectList) { theQueue.Add(obj); } // mark the queue as complete for adding. // Which means that the background workers will know to exit if // there are no more items in the queue. (i.e. they won't wait for more items) theQueue.CompleteAdding(); // At this point, all the background workers are processing the queue // and the main thread is free to continue. // The background workers will all notify the completed event when they're done. </code></pre> <p>The RunWorkerAsync method for each worker reads the queue until it's empty, like this:</p> <pre><code>foreach (var obj in theQueue.GetConsumingEnumerable()) { // process object } </code></pre> <p>As I said, you can do the same thing with Tasks as well.</p> <p>Another option is to start a single background task that makes asynchronous Web requests and limits the number of concurrent requests using a semaphore. Don't have an example for that one offhand.</p>
 

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