Re: Queue object with strange behaviour??

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



Hello Marc. Thank you for replying. There is no locking or Mutexes in this
class. Here is a sample of where the queue empties itself:

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)
{
// 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
}

}

Thread.Sleep(10); // give the processor a break :-}
}
} // end LoggingWithThreads ()
..........................
then we have:
public void LogData(String data)
{
......... // there is some other prep code before this next line .......
LoggingQueue.Enqueue(data); // enqueue data to be logged

} // end LogData ()


Maybe this is a bad way to do it. I am trying to avoid having the program
wait on disk access (since most everything is stored in memory tables) to
write a line of data to the log file.

Is there a cleaver way of doing this?

I don't think I want to lock the semaphore before and after the while loop
because then the program would have to wait until the queue empties to disk
before it can enqueue and proceed.

We are kinda at a loss what to do.

Thanks,

Rob K

"Marc Gravell" wrote:

First question: do you lock the queue (or some other agreed object) while
accessing it? [or mutex, or semaphore, or whatever...] Or is this the result
of chaotic access?

Marc

.