Re: Queue object with strange behaviour??
- From: RobKinney1 <RobKinney1@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 16 Oct 2006 10:45:02 -0700
Wow... After I implented this code, my log file integrity analyzer said there
were 0 errors and everything was logged!!
But I am not sure if I implented this right...:
private void LoggingWithThreads()
{
// run this thread while we have NOT terminated it AND there is
nothing more to be logged
// ... This will run (even if told to terminate) until it has
caught up on the logging.
while (!terminateThread || LoggingQueue.Count > 0)
{
lock (LoggingQueue)
{
if (LoggingQueue.Count <= 0)
{
Monitor.Wait(LoggingQueue);
continue;
}
// purge the queue until there is nothing more to write,
then sleep and check all over again
// if there is anything to spit out to the log file
while (LoggingQueue.Count > 0)
{
try
{
sw.WriteLine(LoggingQueue.Dequeue().ToString());
sw.Flush();
}
catch
{
// do nothing because we don't want to abort the
program just because it couldn't
// write 1 line of log code
}
} // end while()
} // end lock
//Thread.Sleep(10); // give the processor a break :-}
}
} // end LoggingWithThreads ()
..... and the enqueue is:
public void LogData(String data)
{
lock (LoggingQueue)
{
LoggingQueue.Enqueue(addToQueue); // enqueue data to
be logged
if (LoggingQueue.Count >= 1)
{
Monitor.Pulse(LoggingQueue);
}
} // end lock
} // end LogData ()
Is this correct? I don't quite understand how it is working, but somehow it
is.
It seems though that there is a potential, for example, 2000 lines to back
up... and then the main program will have to wait until the queue unloads to
disk. Are we looking at this correctly?
Thanks again Marc. We REALLY appreciate the help here.
Rob K
"Marc Gravell" wrote:
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: Marc Gravell
- 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??
- From: Marc Gravell
- Re: Queue object with strange behaviour??
- Prev by Date: Re: Progress bar in composite control
- Next by Date: Re: Application.Run() problem
- Previous by thread: Re: Queue object with strange behaviour??
- Next by thread: Re: Queue object with strange behaviour??
- Index(es):
Relevant Pages
|