Re: Queue object with strange behaviour??
- From: "Marc Gravell" <marc.gravell@xxxxxxxxx>
- Date: Mon, 16 Oct 2006 17:30:10 +0100
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
.
- Follow-Ups:
- Re: Queue object with strange behaviour??
- From: RobKinney1
- Re: Queue object with strange behaviour??
- References:
- Re: Queue object with strange behaviour??
- From: Marc Gravell
- Re: Queue object with strange behaviour??
- From: RobKinney1
- Re: Queue object with strange behaviour??
- Prev by Date: DataGridView HELP
- Next by Date: Re: System.InvalidOperationException will be thrown if I call the method DataGridView.Rows.Add, How to resolve?
- Previous by thread: Re: Queue object with strange behaviour??
- Next by thread: Re: Queue object with strange behaviour??
- Index(es):