Re: Queue object with strange behaviour??

Tech-Archive recommends: Fix windows errors by optimizing your registry



Then add some ;-p It really, *really* needs it.

Another advantage is that you can use Monitor.Pulse and Monitor.Wait to do
away with CPU hammering... i.e. your logging thread does something like
(don't quote me)

while(!terminate) {
T item;
lock(queue) {
if(queue.Count == 0) {
Monitor.Wait(queue); // release and re-aquire when Pulse()d
continue; // back to test condition... don't assume we got something
}
// count should be > 0
item = queue.Dequeue();
}
// log item (outside of lock), then loop
}

the terminate should also Pulse in case we are Wait()ing, which we probably
will be ;-p

the add code would go something like:
lock(queue) {
queue.Enqueue(item);
if(queue.Count==1) { // queue was empty; listener probably Wait()ing
Monitor.Pulse(queue);
}
}

See also Jon Skeet's pages on threading (google will find it). The above is
off the top of my head and not guaranteed. At all ;-p

Marc


.