Getting a "Queue is empty" exception when Queue.Count > 0

Hi all,

I have the following simple code in my not-so-simple application:

Code Snippet

while (frameQueue.Count > 0)

{

if (boolRunning)

{

ProcessFrame(frameQueue.Dequeue());

...

...

From time to time, the Dequeue method throws an exception saying that the "Queue is empty".

Now, the application is multi-threaded, but dequeuing only occurs at this point, so it doesn't "make sense" (please forgive me for using this term) that the queue was cleared between the Count property test and the call to Dequeue.

Moreover, when the exception is thrown, the value of Count is always >0. Since another thread enqueues the frames, that doesn't say too much, but I wonder if there's any known issue with the Queue collection.

Any help will be greatly appreciated.

Thanks,

Ury

[1237 byte] By [ury] at [2008-1-10]
# 1
Queue is not thread safe. If another thread is Enqueueing, you *must* lock. You are getting this error because the internal state is destroyed.
nobugz at 2007-10-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

Thanks!

Can you please direct me to the "lock" mechanism you're referring to?

ury at 2007-10-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

Code Snippet

object o = new object();

lock (o)

{

// Work with your queue.

}

http://msdn2.microsoft.com/en-us/library/c5kehkcz(vs.71).aspx

good luck

SalvatoreDiFazio at 2007-10-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified