tags:

views:

250

answers:

1

Sometimes, when running a Mono .NET application (it seems to not depend on the input data, as it does not change), I run into following situation: when trying to Dequeue an element from a (non-empty, I check its Count beforehand) Queue, an exception is thrown:

Unhandled Exception: System.InvalidOperationException: Operation is not valid due to the current state of the object at System.Collections.Generic.Queue1[DBWorkItem].Peek ()[0x00000] at System.Collections.Generic.Queue1[DBWorkItem].Dequeue () [0x00000] at DBProcessor.process (System.Object q) [0x0006b] in <...>

Though I can certainly catch this exception, the element is dequeued and lost. Has anyone else encountered this behavior, too?

+4  A: 

Do you have multiple threads using this queue? It may be non-empty when you check the count, but then empty when you try to peek the item.

If you have a short but complete program demonstrating the problem, we could tell for sure. Of course, it's possible that it's a Mono bug, although my experience is that system frameworks are usually more robust than the code calling into them.

Jon Skeet
I am indeed using several threads, however each thread has its own queue (containing work items to be fired against the database), so this should not be a problem. However, I'll try to investigate it.Unfortunately, the code is much too complex to post it here...
DBa
@DBa: Then take the code and hammer it until it's *not* too complex to post here. You *say* that each thread has its own queue - how sure are you that that's really the case? That may be the intention, but if two threads are checking the same queue, that could be the problem.
Jon Skeet