我用c#时写了一个类用来保存日志,大概的做法是:
// write logs to file
public class LogInfo
{
static public LogInfo getInstance() {
if (pLogInfo == null)
pLogInfo = new LogInfo();
return pLogInfo;
}
private LogInfo()
{
iLine = 0;
logFile = new StreamWriter(@"Gobang.log", true);
logFile.AutoFlush = true;
}
~LogInfo()
{
// why this operation can cause something that Windows regards as a problem?
logFile.Close();
}
public void WriteLog(string strInfo)
{
logFile.WriteLine(iLine + " " + strInfo);
iLine++;
}
public void WriteLogFlush(string strInfo)
{
WriteLog(strInfo);
}
private static int iLine;
private StreamWriter logFile;
static private LogInfo pLogInfo;
}
因为在析构时关闭StreamWriter的操作,每次在关闭该windows form的程序时windows都提示该程序正在关闭,并且调用“find a solution to the problem..."的处理机制。有人知道是为什么吗?