C# Queue being accessed from 1 thread.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xMetalDetectorx
    New Member
    • Oct 2008
    • 44

    C# Queue being accessed from 1 thread.

    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.

    Code:
    //inside Thread
    while (myQueue.Count > 0)
    {
       obj = myQueue.Dequeue();
       //process obj
    }
    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
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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?

    Comment

    Working...