我正在做一个简单的班子,其中包括一部精炼钢板。
class Logger
{
private StreamWriter sw;
private DateTime LastTime;
public Logger(string filename)
{
LastTime = DateTime.Now;
sw = new StreamWriter(filename);
}
public void Write(string s)
{
sw.WriteLine((DateTime.Now-LastTime).Ticks/10000+":"+ s);
LastTime = DateTime.Now;
}
public void Flush()
{
sw.Flush();
}
~Logger()
{
sw.Close();//Raises Exception!
}
}
但是,当我结束这名骑自行车的人时,这提出了一个例外,即已经删除了该推器?
Why? And how to make it work such that when the Logger class is deleted, the StreamWriter is closed before deletion?
感谢!