title: ConcurrentQueue with multithreading
body: <p>I am new to multi threading concepts. I need to add certain number of strings to a queue and process them with multiple threads. Using <code>ConcurrentQueue</code> which is thread safe.</p>
  
  <p>This is what I have tried. But all the items added into concurrent queue are not processed. only first 4 items are processed.</p>
  
  <pre><code>class Program
  {
      ConcurrentQueue&lt;string&gt; iQ = new ConcurrentQueue&lt;string&gt;();
      static void Main(string[] args)
      {
          new Program().run();
      }
  
      void run()
      {
          int threadCount = 4;
          Task[] workers = new Task[threadCount];
  
          for (int i = 0; i &lt; threadCount; ++i)
          {
              int workerId = i;
              Task task = new Task(() =&gt; worker(workerId));
              workers[i] = task;
              task.Start();
          }
  
          for (int i = 0; i &lt; 100; i++)
          {
              iQ.Enqueue("Item" + i);
          }
  
          Task.WaitAll(workers);
          Console.WriteLine("Done.");
  
          Console.ReadLine();
      }
  
      void worker(int workerId)
      {
          Console.WriteLine("Worker {0} is starting.", workerId);
          string op;
          if(iQ.TryDequeue(out op))
          {
              Console.WriteLine("Worker {0} is processing item {1}", workerId, op);
          }
  
          Console.WriteLine("Worker {0} is stopping.", workerId);
      }
  
  
  }
  </code></pre>