title: Parallel Programming C# - TPL - Update Shared Variable & Suggestions
body: <p>I am new to Parallel Programming and infact this is the first time I am trying it. I am currently doing a project in .NET 4 and prefer to do have 4 or 5 parallel executions.</p>
  
  <p>I see some options. There is <code>Task.Factory.StartNew</code> <code>Parallel.For</code> <code>Parallel.ForEach</code> etc. </p>
  
  <p>What I am going to do is post to a web-site and fetch the responses for about 200 URLs.</p>
  
  <p>When I use <code>Parallel.ForEach</code> I didn't find a way to control the number of threads and the application went using 130+ threads and the website went unresponsive :)</p>
  
  <p>I am interested in using <code>Task.Factory.StartNew</code> within a for loop and divide the URLs in to 4 or 5 tasks.</p>
  
  <pre><code>List&lt;Task&gt; tasks = new List&lt;Task&gt;();
  for (int i = 0; i &lt; 5; i++)
  {
      List&lt;string&gt; UrlForTask = GetUrlsForTask(i,5); //Lets say will return some thing like 1 of 5 of the list of URLs
      int j = i;
      var t = Task.Factory.StartNew(() =&gt;
      {
          List&lt;PageSummary&gt; t = GetSummary(UrlForTask);
          Summary.AddRange(t); //Summary is a public variable
      }
      tasks.Add(t);
  }
  </code></pre>
  
  <p>I believe that these Tasks kind of boil down to threads. So if I make <code>Summary</code> a <code>List&lt;PageSummary&gt;</code> will it be kind of thread safe (I understand there are issues accessing a shared variable by multiple threads)?</p>
  
  <p>Is this where we should use <code>ConcurrentQueue&lt;T&gt;</code> ?</p>
  
  <p>Do you know of a good resource that helps to learn about accessing and updating a shared variable by multiple tasks etc?</p>
  
  <p>What is the best way I could use for this type of task as you may think ?</p>