A
Andrew Falanga
Hi,
I've implemented a logging class for my program that uses the
Singleton pattern (discussed here: http://msdn.microsoft.com/en-us/library/ms998558.aspx).
Now, my log file uses a StreamWriter class object for the file. I'd
like to know how I can reliably call the StreamWriter.Close() function
to make this flush all it's buffers, etc., before exiting the program.
Originally, everything was working but when the program exited, my
files were 0 bytes in length. I wrote a smaller program trying to use
the StreamWriter class (I'm not well versed in C# at all), and I was
using it in the exact same way and everything worked. So, I then
turned my attention to looking at member functions like Flush or Close
and found them in the class definition.
First, I tried making a ctor in my class that called the Close() when
the object was destroyed, but I got an unhandled exception and went to
the debugger. Turns out that Garbage Collection had already destroyed
my object and the dtor was trying to destroy something that didn't
exist. So now I have the Flush() function called in every one of my
class member functions that write new data to the file. This works,
i.e. my data is written to the file, but how could I make this happen
only once in a destructor or something similar?
Andy
I've implemented a logging class for my program that uses the
Singleton pattern (discussed here: http://msdn.microsoft.com/en-us/library/ms998558.aspx).
Now, my log file uses a StreamWriter class object for the file. I'd
like to know how I can reliably call the StreamWriter.Close() function
to make this flush all it's buffers, etc., before exiting the program.
Originally, everything was working but when the program exited, my
files were 0 bytes in length. I wrote a smaller program trying to use
the StreamWriter class (I'm not well versed in C# at all), and I was
using it in the exact same way and everything worked. So, I then
turned my attention to looking at member functions like Flush or Close
and found them in the class definition.
First, I tried making a ctor in my class that called the Close() when
the object was destroyed, but I got an unhandled exception and went to
the debugger. Turns out that Garbage Collection had already destroyed
my object and the dtor was trying to destroy something that didn't
exist. So now I have the Flush() function called in every one of my
class member functions that write new data to the file. This works,
i.e. my data is written to the file, but how could I make this happen
only once in a destructor or something similar?
Andy