Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See the demo code below. This is mostly untested, and certainly isn't 'production standard', but it should give you a good start!</p> <ol> <li><p>It uses a <code>ConcurrentQueue</code> to hold the list of URLs to be processed. This is threadsafe, and makes life a lot easier.</p></li> <li><p>It has a configurable number of urls and tasks. It's best not to make 1000 tasks, but instead have a queue of work items, and a smaller pool of Tasks which 'pull items' off the queue until it's empty. This means you can performance test different numbers of Tasks and find the best value for your problem.</p></li> <li><p>It uses Invoke when updating the progress bar - this avoids the cross-thread exception.</p></li> <li><p>No <code>BackgroundWorker</code> - just <code>TaskFactory</code> and <code>Task</code></p> <pre><code>public partial class Form1 : Form { private const int UrlCount = 1000; private const int taskCount = 10; private ConcurrentQueue&lt;string&gt; urlList; private List&lt;Task&gt; taskList; public Form1() { InitializeComponent(); } private void ResetQueue() { // fake up a number of strings to process urlList = new ConcurrentQueue&lt;string&gt;(Enumerable.Range(0, UrlCount) .Select(i =&gt; "http://www." + Guid.NewGuid().ToString() + ".com")); } private void button1_Click(object sender, EventArgs e) { ResetQueue(); var taskFactory = new TaskFactory(); // start a bunch of tasks taskList = Enumerable.Range(0, taskCount).Select(i =&gt; taskFactory.StartNew(() =&gt; ProcessUrl())) .ToList(); } void ProcessUrl() { string current; // keep grabbing items till the queue is empty while (urlList.TryDequeue(out current)) { // run your code FindWIN(current); // invoke here to avoid cross thread issues Invoke((Action)(() =&gt; UpdateProgress())); } } void FindWIN(string url) { // your code here // as a demo, sleep a sort-of-random time between 0 and 100 ms Thread.Sleep(Math.Abs(url.GetHashCode()) % 100); } void UpdateProgress() { // work out what percentage of the queue is processed progressBar1.Value = (int)(100 - ((double)urlList.Count * 100.0 / UrlCount)); } } </code></pre></li> </ol>
 

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