when using the following code i get this Exeption (in release mode with debug mode everything works fine):
System.ObjectDisposedException: (German)Auf eine geschlossene Datei kann nicht zugegriff
en werden. (Englisch translation) Can’t access a closed file.
bei System.IO.__Error.FileNotOpen()
bei System.IO.FileStream.Flush()
bei System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
bei System.IO.StreamWriter.Dispose(Boolean disposing)
bei System.IO.StreamWriter.Close()
bei Logdatei.Log.Finalize()

Destructor doesn’t function in release mode
rs12345
LOL :D, Never Mind, Destructor is not needed their in the code, I have just guided him in C++ way so he can understand it better. Destrutor is automatically called by GC and it surpress any calls again to it by using GC.SurpressFinalize(this);
I know I have not written the extreme code but I have tried to explain all the possible condition to make sure resources are released properly. I have mentioned 3 great articles on this Issue and I hope that readers will have a look on them to get Good understanding og GC. And also hope once you thorughly understand the text in he article, you'll never have confusion about resource occupation in mind.
Anyways! Thanks for the comments ;-)
Best Regards,
kirchu
RizwanSharp does a wonderful job -- then slips up at the end. His code never call Dispose() except in the destructor, which means you'll still have the same problem.
You have to call Dispose directly, or have the compiler call it:
{{l.writeError("Fehler");l.writeInfo("Information");FernandoLeite
Hello,
Your coding style is more like C++ programmer, and the way use chosen to release resources is not recommended in .Net. Destructors are not used untill they are really needed and Destructor is Replaced with the Finalizer here in .Net. I'm attaching a code and not explainging it much because its a big topic to covere here. For assistance I'm sharing some very good links for disposal of classes after use. You must read them to follow the right guidlines when you write your code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Log : IDisposable
{
private StreamWriter m_writer;
private bool disposed;
public Log(string path)
{
FileStream f = new FileStream(path, FileMode.Create);
m_writer = new StreamWriter(f);
m_writer.Write("<html><body><h1>LOGFILE</h1><table width=\"100%\" border= \"1\"> <tr><td width= \"10%\"><font color= \"0000FF\">Typ</font></td><td><font color= \"0000FF\">Nachricht</font></td></tr>");
m_writer.Flush();
}
~Log()
{
Dispose(false);
}
public void writeError(String Error)
{
String error = "Error";
m_writer.Write(
"<tr><td width= \"10%\"><font color=\"FF000\">" + error + "</font></td><td>" + Error + "</td></tr>");
m_writer.Flush();
}
public void writeInfo(String Info)
{
String information = "Info";
m_writer.Write(
" <tr><td width= \"10%\"><font color=\"00FF00\">" + information + "</font></td><td>" + Info + "</td></tr>");
m_writer.Flush();
}
public void close()
{
Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
m_writer.Close();
}
disposed = true;
}
}
}
}
And here are the reference links for further reasoning and studies:
http://www.codeproject.com/dotnet/GC_101.asp
http://www.codeproject.com/dotnet/GC_102.asp
http://www.codeproject.com/csharp/gcresdealloc.asp
Wish you all the best ;-)
Best Regards,