title: Using blocking queue along with background worker to access database and download images
body: <p>I have following code that gets images from database and saves it to some folder on your machine.</p>
  
  <pre><code> using (SqlConnection connection = new SqlConnection(connectionstring))
          {
              connection.Open();
              using (SqlCommand cm = new SqlCommand("query", connection))
              {
                  t1.Load(cm.ExecuteReader());
                  foreach (DataRow row in t1.Rows)
                  {
                      fileNames.Add(row["Filename"].ToString());
                  }
              }
          }
          BackgroundWorker bgWorker = (BackgroundWorker)sender;
  
          string baseUrl = "http://some path;
          //int numIterations = 5;
  
          HttpWebRequest request = null;
          foreach (var fileName in fileNames)
          {
              //if (bw.CancellationPending == true)
              //{
              //    e.Cancel = true;
              //    break;
              //}
              //else
              //{
  
                  string url = string.Format(baseUrl, fileName);
                  request = (HttpWebRequest)WebRequest.Create(url);
                  request.Method = "GET";
                  request.ContentType = "application/x-www-form-urlencoded";
                  request.CookieContainer = container;
  
                  response = (HttpWebResponse)request.GetResponse();
                  Stream stream = response.GetResponseStream();
                  Image img = Image.FromStream(stream);
                 /* Queue&lt;System.Drawing.Image&gt; queImage = new Queue&lt;System.Drawing.Image&gt;(10);
  
                   queImage.Add(img); */
  
  
                  img.Save("C:\\some path" + fileName);
  </code></pre>
  
  <p>And this is the code for blocking Queue </p>
  
  <pre><code>    class Queue&lt;T&gt;
          {
              private ConcurrentQueue&lt;T&gt; queue = new ConcurrentQueue&lt;T&gt;();
              private int maxSize;
  
  
              public Queue(int maxSize)
              {
                  this.maxSize = maxSize;
              }
  
              internal void Add(T item)
              {
                  lock (queue)
                  {
                      while (queue.Count &gt;= maxSize)
                      {
                          Monitor.Wait(queue);
                      }
                      queue.Enqueue(item);
                      if (queue.Count == 1)
                      {
                          // wake up any blocked dequeue
                          Monitor.PulseAll(queue);
                      }
                  }
              }
              internal void Remove(T item)
              {
                  lock (queue)
                  {
                      while (queue.Count == 0)
                      {
                          Monitor.Wait(queue);
                      }
  
                      queue.TryDequeue(out item);
                      if (queue.Count == maxSize - 1)
                      {
                          // wake up any blocked enqueue
                          Monitor.PulseAll(queue);
                      }
  
                  }
              }
  </code></pre>
  
  <p>I want to implement multithreading so that images can be accessed faster. Idea is to get the data from database (especially filanames as they are linked to original images) and save them in queue. Let limit of Queue be 20. One thread performs this task. While other thread access images from this queue one by one and perform operation on it.</p>
  
  <p>I have code for blocking queue, which I am not sure if correct. I have tried to implement this queue but I am not pretty much sure as how I should do that. Implementation can be found between ?8...*/ part. I know i have to create a thread using Thread class but not sure which method should be passed as an argument to ThreadStart. Can somebody help me figure out these things?</p>
  
  <ol>
  <li>Where to crate thread and which method to pass?</li>
  <li>How to make use of queue to perform intended operations?</li>
  </ol>