Re: Queue object with strange behaviour??



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

.



Relevant Pages

  • RE: file i/o in multithreaded apps
    ... I'm seeing gaps in the log file using the code I posted - ... the output to a file and that doesn't seem to be missing lines. ... what to lock. ... Get notification to my posts through email? ...
    (microsoft.public.dotnet.framework)
  • Re: How to remove a single line from a flat file (Still very off-topic.)
    ... Every program that writes a log file under UNIX needs a way to split the old from the current. ... Contrast with a file system that actually stores records you can delete. ... Simply open the log file, copy the records you want to "rotate", and delete them off the front of the log file as you go. ... And that lock should be in the file system, rather than as a user-level work-around for a lack of functionality. ...
    (comp.lang.tcl)
  • Re: freezing samba
    ... No occurences of 'opportunistic' and only a commented example contains the ... I get exactly the same error in my log file. ... There are several ways to lock a busy ... > site-wide spam filters at catherders.com. ...
    (comp.os.linux.networking)
  • Re: Copy not copying the whole file
    ... I have an C++ application which writes to a log file. ... Wordpad does lock the file when opening it. ...
    (microsoft.public.windowsxp.basics)