Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Queue being accessed from 1 thread.

Newbie
 
Join Date: Oct 2008
Posts: 19
#1: Oct 8 '08
I have a Windows Form application that has a static Queue collection. At some point, i kick off 1 Thread and dequeue the Queue in it.

Expand|Select|Wrap|Line Numbers
  1. //inside Thread
  2. while (myQueue.Count > 0)
  3. {
  4.    obj = myQueue.Dequeue();
  5.    //process obj
  6. }
My problem is that it doesn't properly dequeue after the first myQueue.Dequeue(). So if I only had one object in that queue, the loop would iterate twice. Why does it do that? Does it have something to do with the fact that I am dequeueing in another thread?



Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Oct 9 '08

re: C# Queue being accessed from 1 thread.


If only one object was in the queue, it should check .Count>0 twice, but should only enter the loop once.
Are you sure it is entering the loop even when the while condition is false?
Reply